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

Adding basic volume syncing #137

Merged
merged 3 commits into from
Oct 31, 2024
Merged

Adding basic volume syncing #137

merged 3 commits into from
Oct 31, 2024

Conversation

MbolotSuse
Copy link
Contributor

Adds syncing for basic volume types (secret/configmap/projected secret and configmap). Also changes the virtual kubelet to use a cache from controller-runtime rather than a client for some operations.

Related to #115.

Adds syncing for basic volume types (secret/configmap/projected secret
and configmap). Also changes the virtual kubelet to use a cache from
controller-runtime rather than a client for some operations.
k3k-kubelet/kubelet.go Show resolved Hide resolved
k3k-kubelet/kubelet.go Outdated Show resolved Hide resolved
k3k-kubelet/kubelet.go Outdated Show resolved Hide resolved
k3k-kubelet/kubelet.go Outdated Show resolved Hide resolved
k3k-kubelet/provider/provider.go Outdated Show resolved Hide resolved
// note: this needs to handle downward api volumes as well, but more thought is needed on how to do that
if volume.ConfigMap != nil {
if err := p.syncConfigmap(ctx, podNamespace, volume.ConfigMap.Name); err != nil {
return fmt.Errorf("unable to sync configmap volume %s: %w", volume.Name, err)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not really sure what is the correct way here but since we are using zap all around maybe we can use zap.Fields instead of fmt.Errorf, better actually if the logger can be passed to the transformVolumes function and add the fields there so that it can actually appear what kind of resources are we working on, as an example:

https://github.com/rancher/k3k/blob/main/pkg/controller/cluster/pod.go#L65-L66

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't have a problem with things near log statement using zap, but I think that errors in the program shouldn't be coupled tightly to the logger in case we want to change it.

Copy link
Contributor

Choose a reason for hiding this comment

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

There should be 1 way to convey information for readability and consistency. We lose both if there are more than 1 way to relay information. It should use the structured logger.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like I said above, I can definitely change this when logging, but when returning an error internally we should avoid reliance on unnecessary third party libraries. I can make a structured log statement here in addition to the error return if you would prefer.

k3k-kubelet/provider/provider.go Outdated Show resolved Hide resolved
k3k-kubelet/provider/provider.go Outdated Show resolved Hide resolved
k3k-kubelet/translate/host.go Outdated Show resolved Hide resolved
k3k-kubelet/controller/configmap.go Show resolved Hide resolved
return reconcile.Result{}, nil
}
var virtual corev1.ConfigMap
err := s.VirtualClient.Get(ctx, req.NamespacedName, &virtual)
Copy link
Contributor

Choose a reason for hiding this comment

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

This err Val should be scope locked to keep consistent with Go standards and the repo pattern already established.

Name: translated.Name,
}
var host corev1.ConfigMap
err = s.HostClient.Get(ctx, translatedKey, &host)
Copy link
Contributor

Choose a reason for hiding this comment

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

This err Val should be scope locked for the same reasons as above. Also, an if err nil checks should be done first and then operate on the err value conditionals.

k3k-kubelet/controller/configmap.go Show resolved Hide resolved
for key, value := range translated.Labels {
host.Labels[key] = value
}
err = s.HostClient.Update(ctx, &host)
Copy link
Contributor

Choose a reason for hiding this comment

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

Scope lock the error value here and throughout the PR.


// Add adds a given resource to the list of resources that will be synced. Safe to call multiple times for the
// same resource.
func (s *ConfigMapSyncer) AddResource(ctx context.Context, namespace string, name 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.

Remove the type declaration from the first string arg. It's not necessary since the second is a string.

@@ -40,17 +51,31 @@ type Provider struct {
logger zap.SugaredLogger
}

func New(config rest.Config, Namespace string, Name string) (*Provider, error) {
coreClient, err := cv1.NewForConfig(&config)
func New(hostConfig rest.Config, hostMgr manager.Manager, virtualMgr manager.Manager, Namespace string, Name string) (*Provider, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove first type declaration for the manager.

if err != nil {
return fmt.Errorf("unable to sync volumes for pod %s/%s: %w", pod.Namespace, pod.Name, err)
}
p.logger.Infof("Creating pod %s/%s for pod %s/%s", tPod.Namespace, tPod.Name, pod.Namespace, pod.Name)
Copy link
Contributor

Choose a reason for hiding this comment

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

This should use positional refs.

// note: this needs to handle downward api volumes as well, but more thought is needed on how to do that
if volume.ConfigMap != nil {
if err := p.syncConfigmap(ctx, podNamespace, volume.ConfigMap.Name); err != nil {
return fmt.Errorf("unable to sync configmap volume %s: %w", volume.Name, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be 1 way to convey information for readability and consistency. We lose both if there are more than 1 way to relay information. It should use the structured logger.

k3k-kubelet/provider/provider.go Outdated Show resolved Hide resolved
// getSecretsAndConfigmaps retrieves a list of all secrets/configmaps that are in use by a given pod. Useful
// for removing/seeing which virtual cluster resources need to be in the host cluster.
func getSecretsAndConfigmaps(pod *corev1.Pod) ([]string, []string) {
secrets := []string{}
Copy link
Contributor

Choose a reason for hiding this comment

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

var secrets []string for here and below. This is the standard the repo is putting forth.

Copy link
Collaborator

@galal-hussein galal-hussein left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the PR!

@MbolotSuse MbolotSuse merged commit 26a7fa0 into rancher:main Oct 31, 2024
1 check passed
briandowns pushed a commit to briandowns/k3k that referenced this pull request Dec 4, 2024
* Adding basic volume syncing

Adds syncing for basic volume types (secret/configmap/projected secret
and configmap). Also changes the virtual kubelet to use a cache from
controller-runtime rather than a client for some operations.
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.

3 participants