Skip to content

Commit

Permalink
updates to readability, standard practices, optimizations, etc
Browse files Browse the repository at this point in the history
Signed-off-by: Brian Downs <[email protected]>
  • Loading branch information
briandowns committed Nov 28, 2024
1 parent c561b03 commit 6f69455
Show file tree
Hide file tree
Showing 27 changed files with 352 additions and 285 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ $(TARGETS): .dapper

.DEFAULT_GOAL := default

.PHONY: $(TARGETS)
.PHONY: $(TARGETS)
5 changes: 3 additions & 2 deletions cli/cmds/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ var (

func create(clx *cli.Context) error {
ctx := context.Background()

if err := validateCreateFlags(); err != nil {
return err
}
Expand Down Expand Up @@ -226,8 +227,8 @@ func validateCreateFlags() error {
if cmds.Kubeconfig == "" && os.Getenv("KUBECONFIG") == "" {
return errors.New("empty kubeconfig")
}
if mode != "shared" && mode != "virtual" {
return errors.New(`mode should be one of "shared" or "virtual"`)
if mode != k3kcluster.OperatingModeShared && mode != k3kcluster.OperatingModeVirtual {
return errors.New("mode should be one of " + k3kcluster.OperatingModeShared + " or " + k3kcluster.OperatingModeVirtual)
}

return nil
Expand Down
1 change: 1 addition & 0 deletions cli/cmds/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,6 @@ func delete(clx *cli.Context) error {
Namespace: cmds.Namespace(),
},
}

return ctrlClient.Delete(ctx, &cluster)
}
6 changes: 5 additions & 1 deletion cli/cmds/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func NewCommand() cli.Command {
}

func generate(clx *cli.Context) error {
var cluster v1alpha1.Cluster
ctx := context.Background()

restConfig, err := clientcmd.BuildConfigFromFlags("", cmds.Kubeconfig)
Expand All @@ -107,6 +106,8 @@ func generate(clx *cli.Context) error {
if err != nil {
return err
}

var cluster v1alpha1.Cluster
clusterKey := types.NamespacedName{
Name: name,
Namespace: cmds.Namespace(),
Expand All @@ -126,12 +127,14 @@ func generate(clx *cli.Context) error {
if org == nil {
org = cli.StringSlice{user.SystemPrivilegedGroup}
}

cfg := kubeconfig.KubeConfig{
CN: cn,
ORG: org,
ExpiryDate: time.Hour * 24 * time.Duration(expirationDays),
AltNames: certAltNames,
}

logrus.Infof("waiting for cluster to be available..")
var kubeconfig []byte
if err := retry.OnError(controller.Backoff, apierrors.IsNotFound, func() error {
Expand All @@ -143,6 +146,7 @@ func generate(clx *cli.Context) error {
}); err != nil {
return err
}

pwd, err := os.Getwd()
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions cli/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ func Namespace() string {
if namespace == "" {
return defaultNamespace
}

return namespace
}
2 changes: 1 addition & 1 deletion k3k-kubelet/README.md → docs/shared_mode.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Virtual Kubelet

This package provides an impelementation of a virtual cluster node using [virtual-kubelet](https://github.com/virtual-kubelet/virtual-kubelet).
The shared mode of k3k is an impelementation of a virtual cluster node using [virtual-kubelet](https://github.com/virtual-kubelet/virtual-kubelet).

The implementation is based on several projects, including:
- [Virtual Kubelet](https://github.com/virtual-kubelet/virtual-kubelet)
Expand Down
3 changes: 3 additions & 0 deletions k3k-kubelet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (c *config) unmarshalYAML(data []byte) error {
if c.AgentIP == "" {
c.AgentIP = conf.AgentIP
}

return nil
}

Expand All @@ -67,6 +68,7 @@ func (c *config) validate() error {
if c.AgentHostname == "" {
return errors.New("agent Hostname is not provided")
}

return nil
}

Expand All @@ -79,5 +81,6 @@ func (c *config) parse(path string) error {
if err != nil {
return err
}

return c.unmarshalYAML(b)
}
61 changes: 30 additions & 31 deletions k3k-kubelet/controller/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"context"
"fmt"
"sync"

"github.com/rancher/k3k/pkg/controller"
Expand All @@ -17,7 +16,7 @@ import (
)

type ConfigMapSyncer struct {
mutex sync.RWMutex
mu sync.RWMutex
// VirtualClient is the client for the virtual cluster
VirtualClient client.Client
// CoreClient is the client for the host cluster
Expand All @@ -41,16 +40,14 @@ func (c *ConfigMapSyncer) Reconcile(ctx context.Context, req reconcile.Request)
var virtual corev1.ConfigMap

if err := c.VirtualClient.Get(ctx, req.NamespacedName, &virtual); err != nil {
return reconcile.Result{
Requeue: true,
}, fmt.Errorf("unable to get configmap %s/%s from virtual cluster: %w", req.Namespace, req.Name, err)
return reconcile.Result{Requeue: true}, err
}

translated, err := c.TranslateFunc(&virtual)
if err != nil {
return reconcile.Result{
Requeue: true,
}, fmt.Errorf("unable to translate configmap %s/%s from virtual cluster: %w", req.Namespace, req.Name, err)
return reconcile.Result{Requeue: true}, err
}

translatedKey := types.NamespacedName{
Namespace: translated.Namespace,
Name: translated.Name,
Expand All @@ -61,29 +58,28 @@ func (c *ConfigMapSyncer) Reconcile(ctx context.Context, req reconcile.Request)
err = c.HostClient.Create(ctx, translated)
// for simplicity's sake, we don't check for conflict errors. The existing object will get
// picked up on in the next re-enqueue
return reconcile.Result{
Requeue: true,
}, fmt.Errorf("unable to create host configmap %s/%s for virtual configmap %s/%s: %w",
translated.Namespace, translated.Name, req.Namespace, req.Name, err)
return reconcile.Result{Requeue: true}, err
}
return reconcile.Result{Requeue: true}, fmt.Errorf("unable to get host configmap %s/%s: %w", translated.Namespace, translated.Name, err)

return reconcile.Result{Requeue: true}, err
}
// we are going to use the host in order to avoid conflicts on update
host.Data = translated.Data
if host.Labels == nil {
host.Labels = make(map[string]string, len(translated.Labels))
}

// we don't want to override labels made on the host cluster by other applications
// but we do need to make sure the labels that the kubelet uses to track host cluster values
// are being tracked appropriately
for key, value := range translated.Labels {
host.Labels[key] = value
}

if err = c.HostClient.Update(ctx, &host); err != nil {
return reconcile.Result{
Requeue: true,
}, fmt.Errorf("unable to update host configmap %s/%s for virtual configmap %s/%s: %w",
translated.Namespace, translated.Name, req.Namespace, req.Name, err)
Requeue: true,
}, err

}
return reconcile.Result{}, nil
Expand All @@ -92,8 +88,8 @@ func (c *ConfigMapSyncer) Reconcile(ctx context.Context, req reconcile.Request)
// isWatching is a utility method to determine if a key is in objs without the caller needing
// to handle mutex lock/unlock.
func (c *ConfigMapSyncer) isWatching(key types.NamespacedName) bool {
c.mutex.RLock()
defer c.mutex.RUnlock()
c.mu.RLock()
defer c.mu.RUnlock()
return c.objs.Has(key)
}

Expand All @@ -108,19 +104,21 @@ func (c *ConfigMapSyncer) AddResource(ctx context.Context, namespace, name strin
if c.isWatching(objKey) {
return nil
}

// lock in write mode since we are now adding the key
c.mutex.Lock()
c.mu.Lock()
if c.objs == nil {
c.objs = sets.Set[types.NamespacedName]{}
}
c.objs = c.objs.Insert(objKey)
c.mutex.Unlock()
_, err := c.Reconcile(ctx, reconcile.Request{
c.mu.Unlock()

if _, err := c.Reconcile(ctx, reconcile.Request{
NamespacedName: objKey,
})
if err != nil {
return fmt.Errorf("unable to reconcile new object %s/%s: %w", objKey.Namespace, objKey.Name, err)
}); err != nil {
return err
}

return nil
}

Expand All @@ -141,26 +139,27 @@ func (c *ConfigMapSyncer) RemoveResource(ctx context.Context, namespace, name st
}, func() error {
return c.removeHostConfigMap(ctx, namespace, name)
}); err != nil {
return fmt.Errorf("unable to remove configmap: %w", err)
return err
}
c.mutex.Lock()
c.mu.Lock()
if c.objs == nil {
c.objs = sets.Set[types.NamespacedName]{}
}
c.objs = c.objs.Delete(objKey)
c.mutex.Unlock()
c.mu.Unlock()
return nil
}

func (c *ConfigMapSyncer) removeHostConfigMap(ctx context.Context, virtualNamespace, virtualName string) error {
var vConfigMap corev1.ConfigMap
err := c.VirtualClient.Get(ctx, types.NamespacedName{Namespace: virtualNamespace, Name: virtualName}, &vConfigMap)
if err != nil {
return fmt.Errorf("unable to get virtual configmap %s/%s: %w", virtualNamespace, virtualName, err)
if err := c.VirtualClient.Get(ctx, types.NamespacedName{Namespace: virtualNamespace, Name: virtualName}, &vConfigMap); err != nil {
return err
}

translated, err := c.TranslateFunc(&vConfigMap)
if err != nil {
return fmt.Errorf("unable to translate virtual secret: %s/%s: %w", virtualNamespace, virtualName, err)
return err
}

return c.HostClient.Delete(ctx, translated)
}
18 changes: 10 additions & 8 deletions k3k-kubelet/controller/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"errors"
"fmt"
"sync"

Expand Down Expand Up @@ -55,6 +56,7 @@ func (c *ControllerHandler) AddResource(ctx context.Context, obj client.Object)
}
// we need to manually lock/unlock since we intned on write locking to add a new controller
c.RUnlock()

var r updateableReconciler
switch obj.(type) {
case *v1.Secret:
Expand All @@ -68,7 +70,7 @@ func (c *ControllerHandler) AddResource(ctx context.Context, obj client.Object)
c.Translater.TranslateTo(s)
return s, nil
},
Logger: c.Logger,
logger: c.Logger,
}
case *v1.ConfigMap:
r = &ConfigMapSyncer{
Expand All @@ -82,17 +84,16 @@ func (c *ControllerHandler) AddResource(ctx context.Context, obj client.Object)
Logger: c.Logger,
}
default:
// TODO: Technically, the configmap/secret syncers are relatively generic, and this
// logic could be used for other types.
return fmt.Errorf("unrecognized type: %T", obj)

}
err := ctrl.NewControllerManagedBy(c.Mgr).

if err := ctrl.NewControllerManagedBy(c.Mgr).
For(&v1.ConfigMap{}).
Complete(r)
if err != nil {
return fmt.Errorf("unable to start configmap controller: %w", err)
Complete(r); err != nil {
return err
}

c.Lock()
if c.controllers == nil {
c.controllers = map[schema.GroupVersionKind]updateableReconciler{}
Expand All @@ -109,7 +110,8 @@ func (c *ControllerHandler) RemoveResource(ctx context.Context, obj client.Objec
ctrl, ok := c.controllers[obj.GetObjectKind().GroupVersionKind()]
c.RUnlock()
if !ok {
return fmt.Errorf("no controller found for gvk" + obj.GetObjectKind().GroupVersionKind().String())
return errors.New("no controller found for gvk" + obj.GetObjectKind().GroupVersionKind().String())
}

return ctrl.RemoveResource(ctx, obj.GetNamespace(), obj.GetName())
}
Loading

0 comments on commit 6f69455

Please sign in to comment.