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

Added wsl linter, and fixed related issues #275

Merged
merged 2 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ linters:

# extra
- misspell
- wsl
5 changes: 5 additions & 0 deletions cli/cmds/cluster/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func createAction(config *CreateConfig) cli.ActionFunc {

if config.token != "" {
logrus.Infof("Creating cluster token secret")

obj := k3kcluster.TokenSecretObj(config.token, name, cmds.Namespace())
if err := ctrlClient.Create(ctx, &obj); err != nil {
return err
Expand All @@ -116,10 +117,12 @@ func createAction(config *CreateConfig) cli.ActionFunc {
if err != nil {
return err
}

host := strings.Split(url.Host, ":")
if config.kubeconfigServerHost != "" {
host = []string{config.kubeconfigServerHost}
}

cluster.Spec.TLSSANs = []string{host[0]}

if err := ctrlClient.Create(ctx, cluster); err != nil {
Expand All @@ -144,6 +147,7 @@ func createAction(config *CreateConfig) cli.ActionFunc {
cfg := kubeconfig.New()

var kubeconfig *clientcmdapi.Config

if err := retry.OnError(availableBackoff, apierrors.IsNotFound, func() error {
kubeconfig, err = cfg.Extract(ctx, ctrlClient, cluster, host[0])
return err
Expand Down Expand Up @@ -199,6 +203,7 @@ func newCluster(name, namespace string, config *CreateConfig) *v1alpha1.Cluster
if config.storageClassName == "" {
cluster.Spec.Persistence.StorageClassName = nil
}

if config.token != "" {
cluster.Spec.TokenSecretRef = &v1.SecretReference{
Name: k3kcluster.TokenSecretName(name),
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 @@ -57,5 +57,6 @@ func delete(clx *cli.Context) error {
Namespace: cmds.Namespace(),
},
}

return ctrlClient.Delete(ctx, &cluster)
}
8 changes: 6 additions & 2 deletions cli/cmds/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ 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 @@ -115,6 +116,7 @@ func generate(clx *cli.Context) error {
if err != nil {
return err
}

clusterKey := types.NamespacedName{
Name: name,
Namespace: cmds.Namespace(),
Expand All @@ -128,11 +130,12 @@ func generate(clx *cli.Context) error {
if err != nil {
return err
}

host := strings.Split(url.Host, ":")
if kubeconfigServerHost != "" {
host = []string{kubeconfigServerHost}
err := altNames.Set(kubeconfigServerHost)
if err != nil {

if err := altNames.Set(kubeconfigServerHost); err != nil {
return err
}
}
Expand All @@ -154,6 +157,7 @@ func generate(clx *cli.Context) error {
logrus.Infof("waiting for cluster to be available..")

var kubeconfig *clientcmdapi.Config

if err := retry.OnError(controller.Backoff, apierrors.IsNotFound, func() error {
kubeconfig, err = cfg.Extract(ctx, ctrlClient, &cluster, host[0])
return err
Expand Down
2 changes: 2 additions & 0 deletions cli/cmds/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewApp() *cli.App {
if debug {
logrus.SetLevel(logrus.DebugLevel)
}

return nil
}

Expand All @@ -58,5 +59,6 @@ func Namespace() string {
if namespace == "" {
return defaultNamespace
}

return namespace
}
14 changes: 14 additions & 0 deletions k3k-kubelet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,46 +31,59 @@ func (c *config) unmarshalYAML(data []byte) error {
if c.ClusterName == "" {
c.ClusterName = conf.ClusterName
}

if c.ClusterNamespace == "" {
c.ClusterNamespace = conf.ClusterNamespace
}

if c.HostConfigPath == "" {
c.HostConfigPath = conf.HostConfigPath
}

if c.VirtualConfigPath == "" {
c.VirtualConfigPath = conf.VirtualConfigPath
}

if c.KubeletPort == "" {
c.KubeletPort = conf.KubeletPort
}

if c.AgentHostname == "" {
c.AgentHostname = conf.AgentHostname
}

if c.ServiceName == "" {
c.ServiceName = conf.ServiceName
}

if c.Token == "" {
c.Token = conf.Token
}

if c.ServerIP == "" {
c.ServerIP = conf.ServerIP
}

if c.Version == "" {
c.Version = conf.Version
}

return nil
}

func (c *config) validate() error {
if c.ClusterName == "" {
return errors.New("cluster name is not provided")
}

if c.ClusterNamespace == "" {
return errors.New("cluster namespace is not provided")
}

if c.AgentHostname == "" {
return errors.New("agent Hostname is not provided")
}

return nil
}

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

return c.unmarshalYAML(b)
}
29 changes: 26 additions & 3 deletions k3k-kubelet/controller/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,27 @@ func (c *ConfigMapSyncer) Reconcile(ctx context.Context, req reconcile.Request)
// return immediately without re-enqueueing. We aren't watching this resource
return reconcile.Result{}, nil
}

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)
}

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)
}

translatedKey := types.NamespacedName{
Namespace: translated.Namespace,
Name: translated.Name,
}

var host corev1.ConfigMap
if err = c.HostClient.Get(ctx, translatedKey, &host); err != nil {
if apierrors.IsNotFound(err) {
Expand All @@ -66,6 +70,7 @@ func (c *ConfigMapSyncer) Reconcile(ctx context.Context, req reconcile.Request)
}, 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}, fmt.Errorf("unable to get host configmap %s/%s: %w", translated.Namespace, translated.Name, err)
}
// we are going to use the host in order to avoid conflicts on update
Expand All @@ -79,13 +84,14 @@ func (c *ConfigMapSyncer) Reconcile(ctx context.Context, req reconcile.Request)
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)

}

return reconcile.Result{}, nil
}

Expand All @@ -94,6 +100,7 @@ func (c *ConfigMapSyncer) Reconcile(ctx context.Context, req reconcile.Request)
func (c *ConfigMapSyncer) isWatching(key types.NamespacedName) bool {
c.mutex.RLock()
defer c.mutex.RUnlock()

return c.objs.Has(key)
}

Expand All @@ -104,23 +111,29 @@ func (c *ConfigMapSyncer) AddResource(ctx context.Context, namespace, name strin
Namespace: namespace,
Name: name,
}

// if we already sync this object, no need to writelock/add it
if c.isWatching(objKey) {
return nil
}

// lock in write mode since we are now adding the key
c.mutex.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{
NamespacedName: objKey,
})

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

return nil
}

Expand All @@ -143,24 +156,34 @@ func (c *ConfigMapSyncer) RemoveResource(ctx context.Context, namespace, name st
}); err != nil {
return fmt.Errorf("unable to remove configmap: %w", err)
}

c.mutex.Lock()
if c.objs == nil {
c.objs = sets.Set[types.NamespacedName]{}
}

c.objs = c.objs.Delete(objKey)
c.mutex.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 {

key := types.NamespacedName{
Namespace: virtualNamespace,
Name: virtualName,
}

if err := c.VirtualClient.Get(ctx, key, &vConfigMap); err != nil {
return fmt.Errorf("unable to get virtual configmap %s/%s: %w", virtualNamespace, virtualName, err)
}

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

return c.HostClient.Delete(ctx, translated)
}
13 changes: 12 additions & 1 deletion k3k-kubelet/controller/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,22 @@ type updateableReconciler interface {

func (c *ControllerHandler) AddResource(ctx context.Context, obj client.Object) error {
c.RLock()

controllers := c.controllers
if controllers != nil {
if r, ok := c.controllers[obj.GetObjectKind().GroupVersionKind()]; ok {
err := r.AddResource(ctx, obj.GetNamespace(), obj.GetName())
c.RUnlock()

return err
}
}

// 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:
r = &SecretSyncer{
Expand Down Expand Up @@ -89,19 +94,23 @@ func (c *ControllerHandler) AddResource(ctx context.Context, obj client.Object)
// 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).
For(&v1.ConfigMap{}).
Complete(r)

if err != nil {
return fmt.Errorf("unable to start configmap controller: %w", err)
}

c.Lock()
if c.controllers == nil {
c.controllers = map[schema.GroupVersionKind]updateableReconciler{}
}

c.controllers[obj.GetObjectKind().GroupVersionKind()] = r

c.Unlock()

return r.AddResource(ctx, obj.GetNamespace(), obj.GetName())
Expand All @@ -112,8 +121,10 @@ func (c *ControllerHandler) RemoveResource(ctx context.Context, obj client.Objec
c.RLock()
ctrl, ok := c.controllers[obj.GetObjectKind().GroupVersionKind()]
c.RUnlock()

if !ok {
return fmt.Errorf("no controller found for gvk %s", obj.GetObjectKind().GroupVersionKind())
}

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