Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor operator to support several types of resources + propagate labels #38992

Merged
merged 3 commits into from
Mar 7, 2024

Conversation

hugoShaka
Copy link
Contributor

@hugoShaka hugoShaka commented Mar 5, 2024

Changelog: The operator now supports setting labels on Teleport resources. Labels are copied from the Kubernetes CR.

I initially just wanted to add label propagation (which is required for OpenSSH and OpenSSHEICE servers).

However I ran into an issue: not all Teleport resources implement the ResourceWithLabels interface.
Basically we have 4 cases:

  • resources implementing ResourceWithLabels (e.g. User, Role, ...)
  • resources implementing ResourceWithOrigin but not ResourceWithLabels (e.g. GitHub connector, ProvisionToken, ...)
  • resources that have been tweaked to implement only the operator TeleportResource interface (LoginRule)
  • resources implementing Resource153

To support all those resoucres, we can either:

  • edit each resource so they expose the same methods (e.g. ProvisionToken exposes GetLabels instead of GetStaticLabels)
  • wrap each ressource in a wrapper that implements the operator TeleportResource interface
  • add multiple reconcilers, one for each resource kind

I don't think it's sane for the operator to force changes on Teleport resources. Especially as those are known to be non consistent. I also don't want the operator to maintain a copy of each resource, this is not viable on the long term.

For those reasons I chose the 3rd approach and built 3 reconcilers. Then I deducplicated the code to end up with a generic reconciler that can be tuned by passing an adapter. The adapter is an empty struct that has all the methods to extract the information the operator requires from the resource.

The resulting PR is large and heavily uses generics but the amount of logical changes is quite low (the only diff is that we now copy labels). I can walk you through the PR if needed.

With this PR, the operator supports properly any resource implementing types.Resource153, this will be useful to add TeleportBotV1 support.

@hugoShaka hugoShaka force-pushed the hugo/operator-label-support branch from 093c0bb to 40fb3b8 Compare March 5, 2024 21:31
@hugoShaka hugoShaka marked this pull request as ready for review March 6, 2024 13:36
Copy link
Contributor

@strideynet strideynet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the adapter approach - it seems like the best way to decouple from Teleport's internal changes a bit and this looks fairly tidy. Look forward to adding Bots using this.

I wonder if we can apply the learnings from this to the Terraform provider ?

integrations/operator/controllers/reconcilers/base.go Outdated Show resolved Hide resolved
return fmt.Errorf("failed to convert Object into Resource object: %T", obj)
}
k8sResource := newKubeResource[K]()
debugLog.Info(fmt.Sprintf("Converting resource from unstructured to %T", k8sResource))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: it'd be preferable to use structured logging here. The reason you've had to use Sprintf is because they are trying to encourage people to switch to structured logging.

@public-teleport-github-review-bot public-teleport-github-review-bot bot removed the request for review from tigrato March 7, 2024 12:20
…bel propagation

I initially just wanted to add label propagation (which is required for
OpenSSH and OpenSSHEICE servers).

However I ran into an issue: not all Teleport resources implement the
`ResourceWithLabels` interface.  Basically we have 4 cases:
- resources implementing `ResourceWithLabels` (e.g. User, Role, ...)
- resources implementing `ResourceWithOrigin` but not
  `ResourceWithLabels` (e.g. GitHub connector, ProvisionToken, ...)
- resources that have been tweaked to implement only the operator
  `TeleportResource` interface (`LoginRule`)
- resources implementing `Resource153`

To support all those resoucres, we can either:
- edit each resource so they expose the same methods (e.g.
  ProvisionToken exposes `GetLabels` instead of `GetStaticLabels`)
- wrap each ressource in a wrapper that implements the operator
  TeleportResource interface
- add multiple reconcilers, one for each resource kind

I don't think it's sane for the operator to force changes on Teleport
resources. Especially as those are known to be non consistent. I also
don't want the operator to maintain a copy of each resource, this is not
viable on the long term.

For those reasons I chose the 3rd approach and built 3 reconcilers. Then
I deducplicated the code to end up with a generic reconciler that can be
tuned by passing an adapter. The adapter is an empty struct that has all
the methods to extract the information the operator requires from the
resource.

The resulting PR is large and heavily uses generics but the amount of
logical changes is quite low (the only diff is that we now copy labels).
I can walk you through the PR if needed.

With this PR, the operator supports properly any resource implementing
`types.Resource153`, this will be useful to add `TeleportBotV1` support.
@hugoShaka hugoShaka force-pushed the hugo/operator-label-support branch from a86c3ab to 3d55c8a Compare March 7, 2024 20:34
@hugoShaka hugoShaka enabled auto-merge March 7, 2024 20:41
@hugoShaka hugoShaka added this pull request to the merge queue Mar 7, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 7, 2024
@hugoShaka hugoShaka added this pull request to the merge queue Mar 7, 2024
Merged via the queue into master with commit 29e1e5b Mar 7, 2024
34 of 35 checks passed
@hugoShaka hugoShaka deleted the hugo/operator-label-support branch March 7, 2024 21:36
@public-teleport-github-review-bot

@hugoShaka See the table below for backport results.

Branch Result
branch/v15 Failed

hugoShaka added a commit that referenced this pull request Mar 7, 2024
…abels (#38992)

* Refactor operator resource reconciler to support resource153 + add label propagation

I initially just wanted to add label propagation (which is required for
OpenSSH and OpenSSHEICE servers).

However I ran into an issue: not all Teleport resources implement the
`ResourceWithLabels` interface.  Basically we have 4 cases:
- resources implementing `ResourceWithLabels` (e.g. User, Role, ...)
- resources implementing `ResourceWithOrigin` but not
  `ResourceWithLabels` (e.g. GitHub connector, ProvisionToken, ...)
- resources that have been tweaked to implement only the operator
  `TeleportResource` interface (`LoginRule`)
- resources implementing `Resource153`

To support all those resoucres, we can either:
- edit each resource so they expose the same methods (e.g.
  ProvisionToken exposes `GetLabels` instead of `GetStaticLabels`)
- wrap each ressource in a wrapper that implements the operator
  TeleportResource interface
- add multiple reconcilers, one for each resource kind

I don't think it's sane for the operator to force changes on Teleport
resources. Especially as those are known to be non consistent. I also
don't want the operator to maintain a copy of each resource, this is not
viable on the long term.

For those reasons I chose the 3rd approach and built 3 reconcilers. Then
I deducplicated the code to end up with a generic reconciler that can be
tuned by passing an adapter. The adapter is an empty struct that has all
the methods to extract the information the operator requires from the
resource.

The resulting PR is large and heavily uses generics but the amount of
logical changes is quite low (the only diff is that we now copy labels).
I can walk you through the PR if needed.

With this PR, the operator supports properly any resource implementing
`types.Resource153`, this will be useful to add `TeleportBotV1` support.

* Update integrations/operator/controllers/reconcilers/base.go

Co-authored-by: Noah Stride <[email protected]>

* use structured logging

---------

Co-authored-by: Noah Stride <[email protected]>
github-merge-queue bot pushed a commit that referenced this pull request Mar 27, 2024
)

* integrations/operator: Add openSSH and openSSHEICE servers

* lint

* Update integrations/operator/controllers/resources/openssheiceserverv2_controller.go

Co-authored-by: Marco André Dinis <[email protected]>

* rename Openssh to OpenSSH and fix comments

* hide cmdlabels

* Refactor operator to support several types of resources + propagate labels (#38992)

* Refactor operator resource reconciler to support resource153 + add label propagation

I initially just wanted to add label propagation (which is required for
OpenSSH and OpenSSHEICE servers).

However I ran into an issue: not all Teleport resources implement the
`ResourceWithLabels` interface.  Basically we have 4 cases:
- resources implementing `ResourceWithLabels` (e.g. User, Role, ...)
- resources implementing `ResourceWithOrigin` but not
  `ResourceWithLabels` (e.g. GitHub connector, ProvisionToken, ...)
- resources that have been tweaked to implement only the operator
  `TeleportResource` interface (`LoginRule`)
- resources implementing `Resource153`

To support all those resoucres, we can either:
- edit each resource so they expose the same methods (e.g.
  ProvisionToken exposes `GetLabels` instead of `GetStaticLabels`)
- wrap each ressource in a wrapper that implements the operator
  TeleportResource interface
- add multiple reconcilers, one for each resource kind

I don't think it's sane for the operator to force changes on Teleport
resources. Especially as those are known to be non consistent. I also
don't want the operator to maintain a copy of each resource, this is not
viable on the long term.

For those reasons I chose the 3rd approach and built 3 reconcilers. Then
I deducplicated the code to end up with a generic reconciler that can be
tuned by passing an adapter. The adapter is an empty struct that has all
the methods to extract the information the operator requires from the
resource.

The resulting PR is large and heavily uses generics but the amount of
logical changes is quite low (the only diff is that we now copy labels).
I can walk you through the PR if needed.

With this PR, the operator supports properly any resource implementing
`types.Resource153`, this will be useful to add `TeleportBotV1` support.

* Update integrations/operator/controllers/reconcilers/base.go

Co-authored-by: Noah Stride <[email protected]>

* use structured logging

---------

Co-authored-by: Noah Stride <[email protected]>

---------

Co-authored-by: Marco André Dinis <[email protected]>
Co-authored-by: Noah Stride <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants