Commit Graph

159 Commits

Author SHA1 Message Date
Francesco Giudici fda53de0e2
Kubebuilder: fix MachineRegistration search during registration (#280)
* operator: fix MachineSelector controller log

* operator: fix MachineRegistration search in registration

Fixes #279

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-12-02 16:13:34 +01:00
Alexander Demicev 9c212ea32a
Merge all main logic in one file (#270) 2022-12-01 14:59:11 +01:00
Francesco Giudici 8c1e1df563
[controller_runtime] add registration protocol version (#266)
Fixes: #265 

* registration: negotiate registration protocol

* operator: always update the MachineInventory for authenticated clients

* register: rename sendData to sendSMBIOSdata

* register: rework the Register() function

* operator: rework the registration protocol loop

* operator: no need to return the msgType from the registration loop

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-11-29 18:08:15 +01:00
Alexander Demicev 8283d03496
Kubebuilder: Remove unused code (#267)
* Remove unused code

* Run make vendor
2022-11-29 12:29:55 +01:00
Francesco Giudici 5df274078b
[controller_runtime] operator/registration: switch to Kubebuilder client (#256)
* operator/registration: switch to Kubebuilder client

Fixes #239

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* operator/registration: adapt tests to Kubebuilder client

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* unit-tests: vendor controller-runtime fake client

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* operator: add yaml annotations for correct marshalling

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* operator/RBAC: add "get" verb to ServiceAccount resources

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* operator: generate rbac

make generate-manifests
make build-rbac

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* Add elementalcli package

Create a separate package to deal with elemental-cli installation.
This brings the elemental-cli functions declared in the config package
to a new package that just parses a map[string]interface argument.

This is a step to enable usage of the elemental-cli functions with the
new elementalv1.Config.Elemental.Install type.

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* register: switch to Kubebuilder api

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* register: add mapstructure annotations for correct marshalling

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* register: reduce complexity of the run function

Make linter happy:
"cyclomatic complexity 16 of func `run` is high (> 15) (gocyclo)"

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* register: drop io/ioutil in favor of os package

io/ioutil is deprecated

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-11-28 17:34:47 +01:00
David Cassany Viladomat 5757f2d82f
Add a rate limiter to managedosversionchannel reconciler (#260)
This commit adds a rate limiter to the ManagedOSVersionChannel controller to prevent
stacking reconcile loops over the same resource in fast rates (doesn't make sense for a
ManagedOSVersionChannel). By default the controller runtime already includes an
equivalent rate limiter, but starts in the range of milliseconds, starting the exponential
rate limiter in the range of seconds is more than enough in this context.

In addition it drops the failures counter in the resource. This counter was supposed to
be used to limit the number attempts to sync in case of failure. This was a bad design,
status should not keep a counter like this as any change in status triggers a new
immediate reconcile loop, hence the counter was reaching the maximum as fast as the
controller runtime was executing reconcile loops without any rate limiter (rate limiter
applies only when there are no changes including status).

For now I think we can just live without the setting any maxium for failures. If we ever
need it I believe it should be coded and tracked within the controller itself, not in each
resource as this prevents the reconcile loop of being idempotent. Alternatively we could
prevent triggering the reconcile loop on status changes, however this prevents
reconciling if any third party (or user from the kubectl client) changes a resource status.

Fixes #257
Part of #240

Signed-off-by: David Cassany <dcassany@suse.com>
2022-11-22 15:40:15 +01:00
David Cassany Viladomat c18f66d8b8
Drop requeuer, not needed anymore (#255)
* Drop requeuer, not needed anymore
* Improve logging

Signed-off-by: David Cassany <dcassany@suse.com>
2022-11-21 12:37:14 +01:00
David Cassany Viladomat 5b68534515
Improve syncer (#252)
This commit adds few changes on the syncer logic:

* Makes use of ManagedOSVersionChannel status reason to track if there
  is an on going synchronization rather than polling for the existence of a synchronization pod or not.

* Adds a logic to stop trying to synchronize after 4 consecutive attempts.
  If it exceeds the maximum it just programs the next re-sync after the given sync
  interval instead of immediately retrying.

* Adds some logging and comments here and there.

Signed-off-by: David Cassany <dcassany@suse.com>
2022-11-18 14:33:12 +01:00
David Cassany Viladomat ed268ce451
New syncer logic (#245)
* Implement syncer logic as part of the ManagedOSVersionChannel controller

  This commit adds the logic to synchronize managedosversionchannels
  within the already existing controller.

* make generate
* make build-manifests
* Update chart
* update e2e tests

Signed-off-by: David Cassany <dcassany@suse.com>
2022-11-16 18:38:23 +01:00
Alexander Demicev db7d0dd68e
Add remaining controllers (#232)
* Update vendor

* Run generation tasks

* Minor fixes in Makefile

* Remove old code

* Add remaning controllers

* Minor e2e tests improvements

* Switch osversionchannel syncer to controll runtime

* Minor fixes in controllers

* Fix unit tests
2022-11-10 11:26:34 +01:00
Alexander Demicev d553a0883d Kubebuilder: Add machine inventory selector controller (#224)
* Add machine inventory selector controller

* Remove old machine inventory selector code
2022-10-27 12:30:03 +02:00
Alexander Demicev 3d5da19c60 Kubebuilder: Add machine inventory controller (#221)
* Add machine inventory controller

* Remove old code

* Add managed label to created secret
2022-10-27 12:29:24 +02:00
Alexander Demicev 0fde15069d Kubebuilder: Add machine registration controller (#206)
* Add new package to Dockerfile

* Update dependencies

* Add unit test helpers

* Add new machine registration controller

* Remove old machine registration controller

* Add rbac tag for secrets

* Fix container argument in chart

* Add labels to all created resources
2022-10-27 12:28:55 +02:00
David Cassany Viladomat ee61cd3bab
Stop elemental-system-agent when the node is ready (#231)
Signed-off-by: David Cassany <dcassany@suse.com>
2022-10-26 18:10:05 +02:00
Francesco Giudici 0c64b49a1c operator: improve logging of the MachineRegistration controller
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-20 09:12:45 +02:00
Francesco Giudici 27b3359122 operator: move ServiceAccount creation to a separate func
The OnChange function of the MachineRegistration controller is becoming
too packed: move the ServiceAccount and associated Secret creation and
management in a separate function

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-20 09:12:45 +02:00
Francesco Giudici a33da016c3 operator: drop duplicated import
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-20 09:12:45 +02:00
Francesco Giudici 05800af134 operator: enforce ServiceAccount's Secret link
If the ServiceAccount for the newly created MachineRegistration
is already there, ensure it has a link to the newly created  Secret

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-20 09:12:45 +02:00
Francesco Giudici 3425236341 operator: create ServiceAccounts before their Secrets
A Secret referencing a missing ServiceAccount will be deleted.
While we create them one after the other, still the safest path is
to create the ServiceAccount first. Otherwise we may be exposed to
a race condition in which:
1. We create the Secret referencing an unexistent ServiceAccount
2. The Secret controller will detect a Secret referencing an unexistent
   ServiceAccount and will mark it for deletion
3. The ServiceAccount is created with the reference to the Secret
4. The Secret gets removed from the controller: the controller also
   updates the ServiceAccount removing the linked Secret

Fixes #197

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-20 09:12:45 +02:00
Francesco Giudici d466e048a7
operator: unit-tests: add coverage for unauthenticatedResponse() (#217)
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-20 08:55:42 +02:00
Francesco Giudici 937d35b527
Elemental Operator: manage empty config in MachineRegistrations (#213)
* operator: manage empty config in MachineRegistrations

We don't deal with empty Spec:Config in MachineRegistrations: in that
case we would end up with a nil Config structure, which we don't check
causing the operator to panic.

Just check and deal with empty (nil) MachineRegistration config.

Fixes #202

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* operator:trivial: rename var machineRegistration to registration

In order to manage a MachineRegistration resource we instantiate a var named
'registration' in all the functions of the server package,  but in the
'unauthenticatedResponse' function.
Let's stay coherent: rename the variable.

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* operator: tests: expand the data structure TestInitNewInventory

This has no functional change: just extend the configuration parameter
that can be set in the data structure used for the tests.
Make use of it in the following commit.

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>

* operator: tests: check empty config in MachineRegistrations

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-18 14:52:36 +02:00
Michal Jura 98c9bff048
Label other objects created by elemental-operator (#216)
Label objects created by elemental-operator with
    "elemental.cattle.io/managed": "true"

It will used by rancher-backup operator to filter these object and
create proper backup from them.

Fixes https://github.com/rancher/elemental/issues/435

Signed-off-by: Michal Jura <mjura@suse.com>

Signed-off-by: Michal Jura <mjura@suse.com>
2022-10-18 14:30:00 +02:00
Michal Jura 3dea2f3f8e
Label secrets managed by elemental-operator (#212)
Mark secrets created and managed by elemental-operator.
It is needed for rancher-backup opeator to select them for backup.

Fixes https://github.com/rancher/elemental/issues/396

Signed-off-by: Michal Jura <mjura@suse.com>

Signed-off-by: Michal Jura <mjura@suse.com>
2022-10-17 07:22:54 +02:00
David Cassany Viladomat cdf406ef81
Allow custom config files for elemental-cli (#210)
* Allow custom config files for elemental-cli
* Update vendor folder

Signed-off-by: David Cassany <dcassany@suse.com>
2022-10-14 10:42:13 +02:00
Francesco Giudici 3a3b4e3c94
operator: drop duplicated import of elemental APIs (#199)
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-10-07 15:25:12 +02:00
Alexander Demicev 8375d083c1
Add reasons for conditions (#185) 2022-10-03 10:47:10 +02:00
Itxaka 72f9347a6b
lint: dont overshadow var (#172)
Signed-off-by: Itxaka <igarcia@suse.com>

Signed-off-by: Itxaka <igarcia@suse.com>
2022-09-26 08:58:23 +00:00
Francesco Giudici 0da0ac126f operator: log the creation of a new registration token
this allow to track the creation of MachineRegistration resources

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-23 15:29:07 +02:00
Francesco Giudici ea712b2c0a operator: explicitly add Secrets to registration ServiceAccounts
With Kubernetes 1.24, creation of ServiceAccounts no more triggers
the creation of an associated Secret resource automatically: we need
it for the ServiceAccount bound to the MachineRegistation resources.
Explicitly create it in any case.

Fixes #176

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-23 15:29:07 +02:00
Francesco Giudici deef7657b6 operator: return error when the ServiceAccount has no secrets
We need to lend the secret to the rancher-system-agent: without that
let's not even start the elemental deployment.

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-23 15:29:07 +02:00
Itxaka 832ee7fad4
tests(registration): More unit tests (#167) 2022-09-07 17:53:24 +02:00
Itxaka 4fab515a30
Rework client to accept a ClientInterface (#166) 2022-09-07 10:39:26 +02:00
Itxaka 3257003185
tests(inventory): Add unit tests for inventory methods (#164) 2022-09-06 11:21:14 +02:00
Francesco Giudici 5da3ac15ff register/operator: drop MachineInventory labels passed from the client
The right way to set labels for the MachineInventory objs is to set
them in the MachineRegistration:spec:machineInventoryLabels, which
will be processed by the operator.
Drop MachineRegistration spec:config:elemental:registration:labels.

Fixes #161

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-06 09:24:14 +02:00
Francesco Giudici 01afffd0e2 unit-tests: check default machine name
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-05 10:59:18 +02:00
Francesco Giudici 41b89ee1ed operator: change default MachineInventory name
right now when the MachineRegistration is missing a 'spec.MachineName'
value we set the following hostname:
m-${System Information/Manufacturer}-${System Information/Product Name}-${System Information/UUID}

This causes an invalid hostname if config.registration.no-smbios is set
to true, failing the registration.
When no-smbios is set to false instead, we may still fail the registration
as the hostname derived by no-smbios data is truncated to 58 chars, which
may end up truncating the UUID, which is the only parameter ensuring uniqueness.

This PR changes the default MachineInventory name to a UUID prepended by "m-":
the UUID is taken from "${System Information/UUID}" when SMBIOS data is available,
otherwise the UUID is randomly generated.

Fixes: https://github.com/rancher/elemental-operator/issues/144

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-05 10:59:18 +02:00
Itxaka 3d28c5d855
Configure custom smbios data (#157) 2022-09-02 14:15:22 +02:00
Francesco Giudici 64f470348a operator: ensure inventory.Labels is not nil before adding labels
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-02 10:05:45 +02:00
Itxaka 75792d6d8d
Add extra labels with smbios data (#155) 2022-09-01 18:15:14 +02:00
Francesco Giudici 03628b79da unit-tests: add websocket coverage
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-01 16:25:59 +02:00
Francesco Giudici 0ffcfe8e83 operator: add unit-test for mergeInventoryLabels()
Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-01 16:25:59 +02:00
Francesco Giudici fa5f689e20 operator/register: drop unused code
remove old code passing data via HTTP labels

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-01 16:25:59 +02:00
Francesco Giudici d49adeb7ed operator/register: rework the registration protocol
This commit changes the communication protocol between the operator and
the elemental-register client in order to take full advantage of the websocket
connection: SMBIOS data and labels are now passed via the websocket channel.
The protocol is now extensible and could be easily extended to pass arbitrary
data or request different kind of services to the operator.

This commit breaks backwards compatibility with older operators and
elemental-register clients.

Fixes #5

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-01 16:25:59 +02:00
Francesco Giudici ebd2c81025 websocket: add helper functions
We are going to rework the communication protocol, making full use
of the bidirectional websocket we set up. Here we introduce some
helper functions that will make the communication easier.

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-01 16:25:59 +02:00
Francesco Giudici 2bcc2ffff6 register: set a timeout for retrieving the installation config
We already had a deadline on the operator side: add to the
registering client to.
Share websocket configuration options as we will soon need it
for establishing a common protocol.

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-09-01 16:25:59 +02:00
Itxaka 364be9d61e
drop unused labels on bootstrap (#154) 2022-09-01 13:40:35 +00:00
Francesco Giudici 4ce5d77900 register: take control of the registration process
Till now, the attestation and communication with the elemental operator
were all demanded to the github.com/rancher-sandbox/go-tpm package.
Split TPM attestation from the communication with the elemental operator
demanding TPM authentication to the external library while taking full
control of the communication with the operator.

This doesn't introduce functional changes (so it will keep retrocompatibility
with the current elemental operator) and is a preparatory step to address
rancher/elemental-operator#5

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-08-31 16:09:20 +02:00
Francesco Giudici 52410aa3b3 fix linter: cyclomatic complexity of ServeHTTP is 16
Error: cyclomatic complexity 16 of func `(*InventoryServer).ServeHTTP` is high (> 15) (gocyclo)

Move code to update labels from headers to subfunction

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-08-31 15:31:03 +02:00
Francesco Giudici 2b646ff145 operator: move websocket management logic out of the tpm package
Till now the TPM package was in charge to upgrade the HTTP connection
and manage the websocket one.
Move the connection management to the server package to split connection
management from TPM authentication.
This will allow us later to pass the smbios data and the labels through
the channel instead of abusing HTTP headers.

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-08-31 15:31:03 +02:00
Francesco Giudici 7192507964 minor: drop duplicated logging
we already log the same error in the calling function
(writeMachineInventoryCloudConfig)

Signed-off-by: Francesco Giudici <francesco.giudici@suse.com>
2022-08-31 15:31:03 +02:00