Path: blob/main/install/installer/pkg/common/objects.go
2500 views
// Copyright (c) 2021 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package common56import (7corev1 "k8s.io/api/core/v1"8metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"9"k8s.io/apimachinery/pkg/runtime"10"k8s.io/apimachinery/pkg/util/intstr"11"k8s.io/utils/pointer"12)1314func DefaultServiceAccount(component string) RenderFunc {15return func(cfg *RenderContext) ([]runtime.Object, error) {16pullSecrets := make([]corev1.LocalObjectReference, 0)1718if len(cfg.Config.ImagePullSecrets) > 0 {19for _, i := range cfg.Config.ImagePullSecrets {20pullSecrets = append(pullSecrets, corev1.LocalObjectReference{21Name: i.Name,22})23}24}2526return []runtime.Object{27&corev1.ServiceAccount{28TypeMeta: TypeMetaServiceAccount,29ObjectMeta: metav1.ObjectMeta{30Name: component,31Namespace: cfg.Namespace,32Labels: CustomizeLabel(cfg, component, TypeMetaConfigmap),33Annotations: CustomizeAnnotation(cfg, component, TypeMetaConfigmap),34},35AutomountServiceAccountToken: pointer.Bool(true),36ImagePullSecrets: pullSecrets,37},38}, nil39}40}4142type ServicePort struct {43Name string44ContainerPort int3245ServicePort int3246}4748func GenerateService(component string, ports []ServicePort, mod ...func(spec *corev1.Service)) RenderFunc {49return func(cfg *RenderContext) ([]runtime.Object, error) {50var servicePorts []corev1.ServicePort51for _, port := range ports {52servicePorts = append(servicePorts, corev1.ServicePort{53Protocol: *TCPProtocol,54Name: port.Name,55Port: port.ServicePort,56TargetPort: intstr.IntOrString{IntVal: port.ContainerPort},57})58}5960// kind=service is required for services. It allows Gitpod to find them61serviceLabels := DefaultLabels(component)62serviceLabels["kind"] = "service"6364service := &corev1.Service{65TypeMeta: TypeMetaService,66ObjectMeta: metav1.ObjectMeta{67Name: component,68Namespace: cfg.Namespace,69Labels: serviceLabels,70Annotations: make(map[string]string),71},72Spec: corev1.ServiceSpec{73Ports: servicePorts,74Selector: DefaultLabels(component),75Type: corev1.ServiceTypeClusterIP,76},77}7879for _, m := range mod {80// Apply any custom modifications to the spec81m(service)82}8384// Add in the customizations for labels and annotations85service.ObjectMeta.Labels = CustomizeLabel(cfg, component, TypeMetaService, func() map[string]string {86return service.ObjectMeta.Labels87})88service.ObjectMeta.Annotations = CustomizeAnnotation(cfg, component, TypeMetaService, func() map[string]string {89return service.ObjectMeta.Annotations90})9192return []runtime.Object{service}, nil93}94}9596// DockerRegistryHash creates a sample pod spec that can be converted into a hash for annotations97func DockerRegistryHash(ctx *RenderContext) ([]runtime.Object, error) {98if !pointer.BoolDeref(ctx.Config.ContainerRegistry.InCluster, false) {99return nil, nil100}101102return []runtime.Object{&corev1.Pod{Spec: corev1.PodSpec{103Containers: []corev1.Container{{104Env: []corev1.EnvVar{{105Name: "DOCKER_REGISTRY_USERNAME",106Value: ctx.Values.InternalRegistryUsername,107}, {108Name: "DOCKER_REGISTRY_PASSWORD",109Value: ctx.Values.InternalRegistryPassword,110}},111}},112}}}, nil113}114115116