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

Service account reconciliation in connect cluster #3187

Merged
merged 9 commits into from
Aug 9, 2023

Conversation

ranatrk
Copy link
Contributor

@ranatrk ranatrk commented Aug 8, 2023

#3086

What changed?

  • bumb k8s.io/apimachinery from v0.26.3 to v0.27.3
  • Add connector package to pkg/api/cluster
  • Add reconcileServiceAccount which
    • Creates service account given a client
    • Creates cluster role and cluster role binding for service account
    • Creates a Service account Secret, which when created is populated with desired token(will be used in the kubeconfig used to access the leaf cluster)

Why was this change made?
Create the resources required (Service account, Cluster Role, Cluster Role Binding, Secret) to be able to access the intended cluster from the management cluster

How was this change implemented?
New package was created that includes the reconciliation process and checks

How did you validate the change?

  • Explain how a reviewer can verify the change themselves
    • A simple driver given an existing kind cluster created that calls the reconciliation function
    • Then using kubectl the generated resources can be verified to exist correctly.
    • Service Account: test-service-account
      Cluster role: test-service-account-cluster-role
      Cluster role binding: test-service-account-cluster-role-binding
      Secret: test-service-account-token
cfg := config.GetConfigOrDie()
clientSet, err := kubernetes.NewForConfig(cfg)
if err != nil {
   panic(err)
}

token, err := connector.ReconcileServiceAccount(context.Background(), clientSet, "test-service-account")
log.Printf("Error: %s", err)
log.Printf("Token :%s", token)
  • Unit tests -- what is covered, what cannot be covered; are
    there tests that fail without the change?

Release notes

Documentation Changes

Other follow ups

@ranatrk ranatrk added the enhancement New feature or request label Aug 8, 2023
@ranatrk ranatrk requested a review from bigkevmcd August 8, 2023 11:50
@ranatrk ranatrk force-pushed the cluster-connection-config branch from 83b8224 to 3a5afc4 Compare August 8, 2023 13:02
Copy link
Contributor

@bigkevmcd bigkevmcd left a comment

Choose a reason for hiding this comment

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

Good start, mostly tidyups and simplifications.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's remove this?

"k8s.io/client-go/tools/clientcmd"
)

// ConnectCluster will ensure that the cluster referenced by newContext can be
Copy link
Contributor

Choose a reason for hiding this comment

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

We can remove this?

And let's add a Doc comment for ConfigForContext?

pkg/cluster/connector/config_test.go Show resolved Hide resolved
t.Fatal(err)
}

if restCfg.Host != "https://cluster2.example.com" {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could also check the other fields in the created config?

// ReconcileServiceAccount accepts a client and the name for a service account.
// A new Service account is created, if one with same name exists that will be used
// A new cluster role and cluster role binding are created, if already existing those will be used
func ReconcileServiceAccount(ctx context.Context, client kubernetes.Interface, serviceAccountName string) ([]byte, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should indicate what it returns ?

return nil
}
for _, resource := range resources {
switch reflect.TypeOf(resource) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use a Go type switch here?

This would simplify the code a fair bit?

// Add resources of different types to the client based on the type of the resource
// Valid resources: v1.ServiceAccount, rbacv1.ClusterRole, rbacv1.ClusterRoleBinding, v1.Secret
func addFakeResources(client kubernetes.Interface, resources []runtime.Object) error {
if resources == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably don't need to do this?

switch reflect.TypeOf(resource) {

case reflect.TypeOf(&v1.ServiceAccount{}):
_, err := client.CoreV1().ServiceAccounts(corev1.NamespaceDefault).Create(context.Background(), resource.(*v1.ServiceAccount), metav1.CreateOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

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

If you pass in a *testing.T you could just fail the test here in the event of error?


}

}
Copy link
Contributor

Choose a reason for hiding this comment

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

Passing in a *testing.T would have another benefit, you could fail if you get an unknown type :-)

return nil
}

func addFakeServiceAccounts(client kubernetes.Interface, serviceAccounts []string) error {
Copy link
Contributor

Choose a reason for hiding this comment

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

@ranatrk ranatrk force-pushed the cluster-connection-config branch 2 times, most recently from 2a6a446 to 9bcf9ad Compare August 9, 2023 15:21
@ranatrk ranatrk requested a review from bigkevmcd August 9, 2023 15:42
Copy link
Contributor

@bigkevmcd bigkevmcd left a comment

Choose a reason for hiding this comment

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

This looks good for the first part, there are some tiny tidyups, but otherwise it looks good.

"k8s.io/client-go/kubernetes"
)

type ClusterConnectionOptions struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a go doc for this, it's exported?
https://github.com/golang/go/wiki/CodeReviewComments#doc-comments

t.Errorf("error adding resources: %v", err)
}
default:
t.Error("invalid resource type")
Copy link
Contributor

Choose a reason for hiding this comment

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

how about t.Fatalf("invalid resource type %s", resource)

This can be fatal, because it's during the setup, so if the setup fails, the tests likely would anyway, for unrelated reasons.

// ReconcileServiceAccount accepts a client and the name for a service account.
// A new Service account is created, if one with same name exists that will be used
// A new cluster role and cluster role binding are created, if already existing those will be used
// returns the token of the secret created for the service account
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit pick here, I would move this to the top of the file, so that it's the first function you see, the rest are fairly unimportant, so it makes it easier to pick up.

But this is very much a personal preference.

bigkevmcd and others added 8 commits August 9, 2023 19:51
Add test cases for existing service account, cluster role, and cluster role binding

Add helper function in test to add resources to client based on the type of the resource

Add logs when existing resources found in reconcile service account instead of creating new ones

Move functionality from service account test to service account
Add test case for failing ConfigForContext

Delete driver file

Add ClusterConnectionOptions struct

Simplify ReconcileServiceAccount to extract resource creation to different helper functions

Cleanup service account tests

Move artificially populating secret token to helper function in test
@ranatrk ranatrk force-pushed the cluster-connection-config branch from 74d0b67 to eb91c05 Compare August 9, 2023 16:51
@ranatrk ranatrk force-pushed the cluster-connection-config branch from eb91c05 to 7ba19b9 Compare August 9, 2023 17:01
@ranatrk ranatrk merged commit fbf1ee9 into main Aug 9, 2023
@ranatrk ranatrk deleted the cluster-connection-config branch August 9, 2023 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants