Skip to content

feat : Add support to add Service annotations from DevWorkspaceRouting configuration (#1293) #1439

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions apis/controller/v1alpha1/devworkspacerouting_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type DevWorkspaceRoutingSpec struct {
Endpoints map[string]EndpointList `json:"endpoints"`
// Selector that should be used by created services to point to the devworkspace Pod
PodSelector map[string]string `json:"podSelector"`
// Machines to services map
// +optional
// +kubebuilder:validation:Optional
Service map[string]Service `json:"services"`
}

type DevWorkspaceRoutingClass string
Expand Down Expand Up @@ -108,6 +112,14 @@ type EndpointProtocol string
// +kubebuilder:validation:XPreserveUnknownFields
type Attributes map[string]apiext.JSON

type Service struct {
// Map of annotations to be added to the Kubernetes Service.
// +optional
// +patchMergeKey=name
// +patchStrategy=merge
Annotations map[string]string `json:"annotations,omitempty"`
}

type Endpoint struct {
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?$
// +kubebuilder:validation:MaxLength=63
Expand Down
29 changes: 29 additions & 0 deletions apis/controller/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ var _ = Describe("DevWorkspaceRouting Controller", func() {
err := k8sClient.Get(ctx, serviceNamespacedName, createdService)
return err == nil
}, timeout, interval).Should(BeTrue(), "Service should exist in cluster")
Expect(createdService.ObjectMeta.Annotations).Should(HaveKeyWithValue(serviceAnnotationKey, serviceAnnotationValue), "Service should have annotation")
Expect(createdService.Spec.Selector).Should(Equal(createdDWR.Spec.PodSelector), "Service should have pod selector from DevWorkspace metadata")
Expect(createdService.Labels).Should(Equal(ExpectedLabels), "Service should contain DevWorkspace ID label")
expectedOwnerReference := devWorkspaceRoutingOwnerRef(createdDWR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,38 @@ var nginxIngressAnnotations = func(endpointName string, endpointAnnotations map[
return annotations
}

func serviceAnnotations(sourceAnnotations map[string]string, isDiscoverable bool, serviceRoutingConfig controllerv1alpha1.Service) map[string]string {
annotations := make(map[string]string)
if sourceAnnotations != nil && len(sourceAnnotations) > 0 {
for k, v := range sourceAnnotations {
annotations[k] = v
}
}
if isDiscoverable {
annotations[constants.DevWorkspaceDiscoverableServiceAnnotation] = "true"
}
if serviceRoutingConfig.Annotations != nil && len(serviceRoutingConfig.Annotations) > 0 {
for k, v := range serviceRoutingConfig.Annotations {
annotations[k] = v
}
}
return annotations
}

// Basic solver exposes endpoints without any authentication
// According to the current cluster there is different behavior:
// Kubernetes: use Ingresses without TLS
// OpenShift: use Routes with TLS enabled
type BasicSolver struct{}

var routingSuffixSupplier = func() string {
return config.GetGlobalConfig().Routing.ClusterHostSuffix
}

var isOpenShift = func() bool {
return infrastructure.IsOpenShift()
}

var _ RoutingSolver = (*BasicSolver)(nil)

func (s *BasicSolver) FinalizerRequired(*controllerv1alpha1.DevWorkspaceRouting) bool {
Expand All @@ -63,16 +89,16 @@ func (s *BasicSolver) GetSpecObjects(routing *controllerv1alpha1.DevWorkspaceRou
routingObjects := RoutingObjects{}

// TODO: Use workspace-scoped ClusterHostSuffix to allow overriding
routingSuffix := config.GetGlobalConfig().Routing.ClusterHostSuffix
routingSuffix := routingSuffixSupplier()
if routingSuffix == "" {
return routingObjects, &RoutingInvalid{"basic routing requires .config.routing.clusterHostSuffix to be set in operator config"}
}

spec := routing.Spec
services := getServicesForEndpoints(spec.Endpoints, workspaceMeta)
services = append(services, GetDiscoverableServicesForEndpoints(spec.Endpoints, workspaceMeta)...)
services := getServicesForEndpoints(spec, workspaceMeta)
services = append(services, GetDiscoverableServicesForEndpoints(spec, workspaceMeta)...)
routingObjects.Services = services
if infrastructure.IsOpenShift() {
if isOpenShift() {
routingObjects.Routes = getRoutesForSpec(routingSuffix, spec.Endpoints, workspaceMeta)
} else {
routingObjects.Ingresses = getIngressesForSpec(routingSuffix, spec.Endpoints, workspaceMeta)
Expand Down
Loading
Loading