From 32b119dcad4358d2bed0456b2633f4ada8e980cc Mon Sep 17 00:00:00 2001 From: tunahanertekin Date: Tue, 15 Aug 2023 11:54:10 +0300 Subject: [PATCH 1/5] refactor(label): add label key for registry --- internal/shared.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/shared.go b/internal/shared.go index 6b4ccbf4..bf895e78 100644 --- a/internal/shared.go +++ b/internal/shared.go @@ -19,6 +19,7 @@ const ( // Ready robot label const ( + ROBOT_IMAGE_REGISTRY = "docker.io" ROBOT_IMAGE_USER = "robolaunch.io/robot-image-user" ROBOT_IMAGE_REPOSITORY = "robolaunch.io/robot-image-repository" ROBOT_IMAGE_TAG = "robolaunch.io/robot-image-tag" From eb4f5761032ec4f1613902e53c6a0c34dec18cd9 Mon Sep 17 00:00:00 2001 From: tunahanertekin Date: Tue, 15 Aug 2023 12:39:24 +0300 Subject: [PATCH 2/5] refactor(webhooks): convert repo path defaulter to method --- .../roboscale.io/v1alpha1/robot_webhook.go | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go index a1c0d743..0f36cd15 100644 --- a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go +++ b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go @@ -33,24 +33,12 @@ var _ webhook.Defaulter = &Robot{} func (r *Robot) Default() { robotlog.Info("default", "name", r.Name) - DefaultRepositoryPaths(r) + r.setRepositoryPaths() _ = r.setRepositoryInfo() r.setWorkspacesPath() r.setDiscoveryServerDomainID() } -func DefaultRepositoryPaths(r *Robot) { - for wsKey := range r.Spec.WorkspaceManagerTemplate.Workspaces { - ws := r.Spec.WorkspaceManagerTemplate.Workspaces[wsKey] - for repoKey := range ws.Repositories { - repo := ws.Repositories[repoKey] - repo.Path = r.Spec.WorkspaceManagerTemplate.WorkspacesPath + "/" + ws.Name + "/src/" + repoKey - ws.Repositories[repoKey] = repo - } - r.Spec.WorkspaceManagerTemplate.Workspaces[wsKey] = ws - } -} - //+kubebuilder:webhook:path=/validate-robot-roboscale-io-v1alpha1-robot,mutating=false,failurePolicy=fail,sideEffects=None,groups=robot.roboscale.io,resources=robots,verbs=create;update,versions=v1alpha1,name=vrobot.kb.io,admissionReviewVersions=v1 var _ webhook.Validator = &Robot{} @@ -217,6 +205,18 @@ func (r *Robot) checkRobotDevSuite() error { return nil } +func (r *Robot) setRepositoryPaths() { + for wsKey := range r.Spec.WorkspaceManagerTemplate.Workspaces { + ws := r.Spec.WorkspaceManagerTemplate.Workspaces[wsKey] + for repoKey := range ws.Repositories { + repo := ws.Repositories[repoKey] + repo.Path = r.Spec.WorkspaceManagerTemplate.WorkspacesPath + "/" + ws.Name + "/src/" + repoKey + ws.Repositories[repoKey] = repo + } + r.Spec.WorkspaceManagerTemplate.Workspaces[wsKey] = ws + } +} + func (r *Robot) setRepositoryInfo() error { for k1, ws := range r.Spec.WorkspaceManagerTemplate.Workspaces { From fe0d1f8abef50576da36458b589ecf9753df4404 Mon Sep 17 00:00:00 2001 From: tunahanertekin Date: Tue, 15 Aug 2023 12:46:13 +0300 Subject: [PATCH 3/5] refactor(webhooks): use docker.io as default registry --- pkg/api/roboscale.io/v1alpha1/robot_webhook.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go index 0f36cd15..31d99fc0 100644 --- a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go +++ b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go @@ -33,6 +33,7 @@ var _ webhook.Defaulter = &Robot{} func (r *Robot) Default() { robotlog.Info("default", "name", r.Name) + r.setDefaultLabels() r.setRepositoryPaths() _ = r.setRepositoryInfo() r.setWorkspacesPath() @@ -205,6 +206,12 @@ func (r *Robot) checkRobotDevSuite() error { return nil } +func (r *Robot) setDefaultLabels() { + r.SetLabels(map[string]string{ + internal.ROBOT_IMAGE_REGISTRY: "docker.io", + }) +} + func (r *Robot) setRepositoryPaths() { for wsKey := range r.Spec.WorkspaceManagerTemplate.Workspaces { ws := r.Spec.WorkspaceManagerTemplate.Workspaces[wsKey] From 4af759d79add36d89f17bdeaff7fa376b8b022a6 Mon Sep 17 00:00:00 2001 From: tunahanertekin Date: Tue, 15 Aug 2023 12:54:11 +0300 Subject: [PATCH 4/5] refactor(image): add registry to direct image definition --- internal/node/image.go | 14 ++++++++------ pkg/api/roboscale.io/v1alpha1/robot_webhook.go | 6 +++--- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/internal/node/image.go b/internal/node/image.go index 8b8fa1e3..c7b09e39 100644 --- a/internal/node/image.go +++ b/internal/node/image.go @@ -56,12 +56,14 @@ type ReadyRobotProperties struct { func GetReadyRobotProperties(robot robotv1alpha1.Robot) ReadyRobotProperties { labels := robot.GetLabels() - if user, hasUser := labels[internal.ROBOT_IMAGE_USER]; hasUser { - if repository, hasRepository := labels[internal.ROBOT_IMAGE_REPOSITORY]; hasRepository { - if tag, hasTag := labels[internal.ROBOT_IMAGE_TAG]; hasTag { - return ReadyRobotProperties{ - Enabled: true, - Image: user + "/" + repository + ":" + tag, + if registry, hasRegistry := labels[internal.ROBOT_IMAGE_REGISTRY]; hasRegistry { + if user, hasUser := labels[internal.ROBOT_IMAGE_USER]; hasUser { + if repository, hasRepository := labels[internal.ROBOT_IMAGE_REPOSITORY]; hasRepository { + if tag, hasTag := labels[internal.ROBOT_IMAGE_TAG]; hasTag { + return ReadyRobotProperties{ + Enabled: true, + Image: registry + "/" + user + "/" + repository + ":" + tag, + } } } } diff --git a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go index 31d99fc0..156cc6f0 100644 --- a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go +++ b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go @@ -207,9 +207,9 @@ func (r *Robot) checkRobotDevSuite() error { } func (r *Robot) setDefaultLabels() { - r.SetLabels(map[string]string{ - internal.ROBOT_IMAGE_REGISTRY: "docker.io", - }) + if _, ok := r.Labels[internal.ROBOT_IMAGE_REGISTRY]; !ok { + r.Labels[internal.ROBOT_IMAGE_REGISTRY] = "docker.io" + } } func (r *Robot) setRepositoryPaths() { From 46656780703eddb50efe54f130f4ac6c33e715e0 Mon Sep 17 00:00:00 2001 From: tunahanertekin Date: Tue, 15 Aug 2023 13:00:15 +0300 Subject: [PATCH 5/5] refactor(image): add registry to image selector function --- internal/node/image.go | 22 ++++++++++++++----- internal/shared.go | 8 +++---- .../roboscale.io/v1alpha1/robot_webhook.go | 4 ++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/internal/node/image.go b/internal/node/image.go index c7b09e39..ab426b6b 100644 --- a/internal/node/image.go +++ b/internal/node/image.go @@ -56,10 +56,10 @@ type ReadyRobotProperties struct { func GetReadyRobotProperties(robot robotv1alpha1.Robot) ReadyRobotProperties { labels := robot.GetLabels() - if registry, hasRegistry := labels[internal.ROBOT_IMAGE_REGISTRY]; hasRegistry { - if user, hasUser := labels[internal.ROBOT_IMAGE_USER]; hasUser { - if repository, hasRepository := labels[internal.ROBOT_IMAGE_REPOSITORY]; hasRepository { - if tag, hasTag := labels[internal.ROBOT_IMAGE_TAG]; hasTag { + if registry, hasRegistry := labels[internal.ROBOT_IMAGE_REGISTRY_LABEL_KEY]; hasRegistry { + if user, hasUser := labels[internal.ROBOT_IMAGE_USER_LABEL_KEY]; hasUser { + if repository, hasRepository := labels[internal.ROBOT_IMAGE_REPOSITORY_LABEL_KEY]; hasRepository { + if tag, hasTag := labels[internal.ROBOT_IMAGE_TAG_LABEL_KEY]; hasTag { return ReadyRobotProperties{ Enabled: true, Image: registry + "/" + user + "/" + repository + ":" + tag, @@ -101,13 +101,18 @@ func GetImageForRobot(node corev1.Node, robot robotv1alpha1.Robot) (string, erro return "", err } + registry, hasRegistry := robot.Labels[internal.ROBOT_IMAGE_REGISTRY_LABEL_KEY] + if !hasRegistry { + return "", errors.New("registry is not found in label with key " + internal.ROBOT_IMAGE_REGISTRY_LABEL_KEY) + } + organization := "robolaunchio" repository := "devspace-robotics" tagBuilder.WriteString(imageProps.Application.Name + "-") tagBuilder.WriteString(imageProps.Application.Version) tagBuilder.WriteString("-" + imageProps.DevSpaceImage.UbuntuDistro + "-" + imageProps.DevSpaceImage.Desktop) tagBuilder.WriteString("-" + imageProps.DevSpaceImage.Version) - imageBuilder.WriteString(filepath.Join(organization, repository) + ":") + imageBuilder.WriteString(filepath.Join(registry, organization, repository) + ":") imageBuilder.WriteString(tagBuilder.String()) } @@ -133,6 +138,11 @@ func GetImageForEnvironment(node corev1.Node, robot robotv1alpha1.Robot) (string return "", err } + registry, hasRegistry := robot.Labels[internal.ROBOT_IMAGE_REGISTRY_LABEL_KEY] + if !hasRegistry { + return "", errors.New("registry is not found in label with key " + internal.ROBOT_IMAGE_REGISTRY_LABEL_KEY) + } + organization := imageProps.Organization repository := imageProps.Repository @@ -201,7 +211,7 @@ func GetImageForEnvironment(node corev1.Node, robot robotv1alpha1.Robot) (string tagBuilder.WriteString(chosenElement.DevSpaceImage.Desktop + "-") tagBuilder.WriteString(chosenElement.DevSpaceImage.Version) - imageBuilder.WriteString(filepath.Join(organization, repository) + ":") + imageBuilder.WriteString(filepath.Join(registry, organization, repository) + ":") imageBuilder.WriteString(tagBuilder.String()) } diff --git a/internal/shared.go b/internal/shared.go index bf895e78..3644b792 100644 --- a/internal/shared.go +++ b/internal/shared.go @@ -19,10 +19,10 @@ const ( // Ready robot label const ( - ROBOT_IMAGE_REGISTRY = "docker.io" - ROBOT_IMAGE_USER = "robolaunch.io/robot-image-user" - ROBOT_IMAGE_REPOSITORY = "robolaunch.io/robot-image-repository" - ROBOT_IMAGE_TAG = "robolaunch.io/robot-image-tag" + ROBOT_IMAGE_REGISTRY_LABEL_KEY = "robolaunch.io/robot-image-registry" + ROBOT_IMAGE_USER_LABEL_KEY = "robolaunch.io/robot-image-user" + ROBOT_IMAGE_REPOSITORY_LABEL_KEY = "robolaunch.io/robot-image-repository" + ROBOT_IMAGE_TAG_LABEL_KEY = "robolaunch.io/robot-image-tag" ) // Target resource labels diff --git a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go index 156cc6f0..7e2d99d6 100644 --- a/pkg/api/roboscale.io/v1alpha1/robot_webhook.go +++ b/pkg/api/roboscale.io/v1alpha1/robot_webhook.go @@ -207,8 +207,8 @@ func (r *Robot) checkRobotDevSuite() error { } func (r *Robot) setDefaultLabels() { - if _, ok := r.Labels[internal.ROBOT_IMAGE_REGISTRY]; !ok { - r.Labels[internal.ROBOT_IMAGE_REGISTRY] = "docker.io" + if _, ok := r.Labels[internal.ROBOT_IMAGE_REGISTRY_LABEL_KEY]; !ok { + r.Labels[internal.ROBOT_IMAGE_REGISTRY_LABEL_KEY] = "docker.io" } }