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

Initialize the cluster client cache from the kubeconfig in secrets #412

Merged

Conversation

metlos
Copy link
Contributor

@metlos metlos commented Jul 12, 2024

This changes the cluster cache to initialize the connections to the clusters from the kubeconfig stored in the toolchain cluster secret.

The information from the cluster cache is also used to populate the new informational fields in the ToolchainCluster status.

Related PRs:

and read the connection configuration from the kubeconfig stored in
the secret.
…itialized

and don't clear out other fields than the conditions in the healtchcheck
from the status.
Copy link

codecov bot commented Jul 12, 2024

Codecov Report

Attention: Patch coverage is 76.74419% with 10 lines in your changes missing coverage. Please review.

Project coverage is 78.57%. Comparing base (cd921d6) to head (0196128).
Report is 1 commits behind head on master.

Files Patch % Lines
pkg/cluster/service.go 72.72% 5 Missing and 4 partials ⚠️
...rs/toolchaincluster/toolchaincluster_controller.go 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #412      +/-   ##
==========================================
+ Coverage   78.54%   78.57%   +0.03%     
==========================================
  Files          49       49              
  Lines        2023     2054      +31     
==========================================
+ Hits         1589     1614      +25     
- Misses        376      379       +3     
- Partials       58       61       +3     
Files Coverage Δ
controllers/toolchaincluster/healthchecker.go 100.00% <ø> (ø)
...rs/toolchaincluster/toolchaincluster_controller.go 83.18% <90.00%> (+0.94%) ⬆️
pkg/cluster/service.go 83.22% <72.72%> (-1.46%) ⬇️

Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

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

I probably know why you kept the support for the legacy config. There could be a problem as soon as you would merge a support for kubeconfig only. The reason is that we load the ToolchainCluster CRs and their configs inside main.go before we start the controllers. This means that the ToolchainCluster controller that does the migration wouldn't be started yet and it wouldn't be able to migrate the legacy fields in the secret to the kubeconfig one.
Anyway, at some point we need to make the switch, so I decided to not keep postponing this action and I updated the add-cluster.sh script so it populates the kubeconfig right away:
codeready-toolchain/toolchain-cicd#117

This means that we are ready to drop the migration logic from the ToolchainCluster controller and support kubeconfig only.

controllers/toolchaincluster/healthchecker.go Outdated Show resolved Hide resolved
pkg/cluster/service.go Show resolved Hide resolved
pkg/cluster/service.go Outdated Show resolved Hide resolved
pkg/cluster/service.go Outdated Show resolved Hide resolved
…n-creds-secret

# Conflicts:
#	controllers/toolchaincluster/healthchecker.go
#	controllers/toolchaincluster/toolchaincluster_controller.go
…creating

the client for the remote clusters.
Copy link
Contributor

@MatousJobanek MatousJobanek left a comment

Choose a reason for hiding this comment

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

Looks good, but since the "kubeconfig` field is the preferred one now, could you please update the unit tests so they load the ToolchainCluster clients from kubeconfig instead of from the legacy fields?
I guess that it should be enough to update this helper function:

func NewToolchainClusterWithEndpoint(name, tcNs, secName, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus, labels map[string]string) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) {
gock.New(apiEndpoint).
Get("api").
Persist().
Reply(200).
BodyString("{}")
secret := &corev1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: secName,
Namespace: tcNs,
},
Type: corev1.SecretTypeOpaque,
Data: map[string][]byte{
"token": []byte("mycooltoken"),
},
}

@metlos
Copy link
Contributor Author

metlos commented Aug 14, 2024

Looks good, but since the "kubeconfig` field is the preferred one now, could you please update the unit tests so they load the ToolchainCluster clients from kubeconfig instead of from the legacy fields? I guess that it should be enough to update this helper function.

For this to make most sense, I guess we should also not use any legacy fields in the code as well as in the tests. When I do that, I get quite a substantial diff (on top of the changes in this PR). The main reason for that diff is that the function you mention now needs to contain more parameters (namely operator namespace and insecureTls) because those are now part of the kubeconfig and cannot be easily set as before when they were just part of the returned toolchain cluster spec.

$ git diff --stat
 controllers/toolchaincluster/healthchecker_test.go               | 22 ++++++++++------------
 controllers/toolchaincluster/toolchaincluster_controller.go      | 41 -----------------------------------------
 controllers/toolchaincluster/toolchaincluster_controller_test.go | 65 ++++++++++++++---------------------------------------------------
 pkg/cluster/service.go                                           | 43 +------------------------------------------
 pkg/cluster/service_test.go                                      | 80 ++++++++------------------------------------------------------------------------
 pkg/cluster/service_whitebox_test.go                             |  8 +++-----
 pkg/test/cluster.go                                              | 49 ++++++++++++++++++++++++++++++++++++++++++++-----
 pkg/test/verify/cluster.go                                       | 80 +++++++++++++++++---------------------------------------------------------------
 8 files changed, 97 insertions(+), 291 deletions(-)

I think it makes more sense to merge this PR as is and move the test updates and removal of the migration logic to a new PR.. WDYT?

Copy link
Contributor

@mfrancisc mfrancisc left a comment

Choose a reason for hiding this comment

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

Looks good !

I have one minor comment only.

})

t.Run("uses kubeconfig in precedence over legacy fields", func(t *testing.T) {
tc := newFormTc()
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to add a test case that uses the legacyTc?

basically:

                 tc := legacyTc()
		// Combine the kubeconfig and the token in the same secret.
		// We should see auth from the kubeconfig used...
		secret := kubeconfigSecret(t)
		secret.Data["token"] = []byte("not-the-token-we-want")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This essentially checks that if we detect a kubeconfig data field in the secret then we use that instead of the data in the ToolchainCluster and the token in the secret.

So as soon as we see the data coming from the kubeconfig, we know that happened.

Now, you have a very good idea for a "defensive test" that would make sure any future changes wouldn't change that behavior. But we plan to completely remove the loading from the legacy fields in the upcoming PRs so I think we can afford to not have that and instead just make sure we use the new over the old.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I was asking since as soon as you push this PR to prod, the tc will still have the legacy format, and later will have the new one. But I guess it's not a concern since the only thing that matters is the loading mechanism and not the tc format.

Copy link

sonarcloud bot commented Aug 26, 2024

@metlos
Copy link
Contributor Author

metlos commented Aug 26, 2024

@metlos metlos merged commit 8833ded into codeready-toolchain:master Aug 26, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants