Skip to content

Commit

Permalink
Use MetadataItems to set labels/annotations for pods
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksej-paschenko committed Jan 11, 2022
1 parent db2ac19 commit 205c450
Show file tree
Hide file tree
Showing 14 changed files with 279 additions and 126 deletions.
4 changes: 2 additions & 2 deletions config/crd/bases/theketch.io_apps.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ spec:
properties:
annotations:
description: Annotations is a list of annotations that will be applied
to Services/Deployments/Gateways.
to Services/Deployments/Pods/Gateways/Ingresses/IngressRoutes.
items:
description: MetadataItem represent a request to add label/annotations
to processes
Expand Down Expand Up @@ -2356,7 +2356,7 @@ spec:
- generateDefaultCname
type: object
labels:
description: Labels is a list of labels that will be applied to Services/Deployments.
description: Labels is a list of labels that will be applied to Services/Deployments/Pods.
items:
description: MetadataItem represent a request to add label/annotations
to processes
Expand Down
10 changes: 7 additions & 3 deletions internal/api/v1beta1/app_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ type ProcessSpec struct {
Volumes []v1.Volume `json:"volumes,omitempty"`

VolumeMounts []v1.VolumeMount `json:"volumeMounts,omitempty"`

// Security options the process should run with.
SecurityContext *v1.SecurityContext `json:"securityContext,omitempty"`
}
Expand Down Expand Up @@ -225,10 +224,10 @@ type AppSpec struct {
// BuildPacks is a list of build packs to use when building from source.
BuildPacks []string `json:"buildPacks,omitempty"`

// Labels is a list of labels that will be applied to Services/Deployments.
// Labels is a list of labels that will be applied to Services/Deployments/Pods.
Labels []MetadataItem `json:"labels,omitempty"`

// Annotations is a list of annotations that will be applied to Services/Deployments/Gateways.
// Annotations is a list of annotations that will be applied to Services/Deployments/Pods/Gateways/Ingresses/IngressRoutes.
Annotations []MetadataItem `json:"annotations,omitempty"`

// ServiceAccountName specifies a service account name to be used for this application.
Expand Down Expand Up @@ -664,6 +663,11 @@ func (t Target) IsService() bool {
return t.Kind == "Service" && t.APIVersion == "v1"
}

// IsPod returns true if the target is a Pod.
func (t Target) IsPod() bool {
return t.Kind == "Pod" && t.APIVersion == "v1"
}

const (
CanaryNotActiveEvent = "CanaryNotActive"
CanaryNotActiveEventDesc = "error - canary triggered, but not active"
Expand Down
35 changes: 22 additions & 13 deletions internal/chart/application_chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,31 @@ func TestNewApplicationChart(t *testing.T) {
{Name: "darkweb.theketch.io", Secure: true, SecretName: "darkweb-ssl"},
},
},
Labels: []ketchv1.MetadataItem{{
Apply: map[string]string{"theketch.io/test-label": "test-label-value"},
DeploymentVersion: 3,
ProcessName: "web",
Target: ketchv1.Target{
APIVersion: "apps/v1",
Kind: "Deployment",
Labels: []ketchv1.MetadataItem{
{
Apply: map[string]string{"pod.io/label": "pod-label"},
DeploymentVersion: 3,
ProcessName: "web",
Target: ketchv1.Target{APIVersion: "v1", Kind: "Pod"},
},
{
Apply: map[string]string{"theketch.io/test-label": "test-label-value"},
DeploymentVersion: 3,
ProcessName: "web",
Target: ketchv1.Target{APIVersion: "apps/v1", Kind: "Deployment"},
},
}, {
Apply: map[string]string{"theketch.io/test-label-all": "test-label-value-all"},
Target: ketchv1.Target{
APIVersion: "apps/v1",
Kind: "Deployment",
{
Apply: map[string]string{"theketch.io/test-label-all": "test-label-value-all"},
Target: ketchv1.Target{APIVersion: "apps/v1", Kind: "Deployment"},
},
}},
},
Annotations: []ketchv1.MetadataItem{
{
Apply: map[string]string{"pod.io/annotation": "pod-annotation"},
DeploymentVersion: 3,
ProcessName: "web",
Target: ketchv1.Target{APIVersion: "v1", Kind: "Pod"},
},
{
Apply: map[string]string{"theketch.io/test-annotation": "test-annotation-value"},
DeploymentVersion: 4,
Expand Down
60 changes: 35 additions & 25 deletions internal/chart/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ type process struct {
PublicServicePort int32 `json:"publicServicePort,omitempty"`
Env []ketchv1.Env `json:"env"`

PodExtra podExtra `json:"extra"`
}

type podExtra struct {
SecurityContext *v1.SecurityContext `json:"securityContext,omitempty"`
ResourceRequirements *v1.ResourceRequirements `json:"resourceRequirements,omitempty"`
NodeSelectorTerms []v1.NodeSelectorTerm `json:"nodeSelectorTerms,omitempty"`
Expand All @@ -36,8 +32,12 @@ type podExtra struct {
ReadinessProbe *v1.Probe `json:"readinessProbe,omitempty"`
LivenessProbe *v1.Probe `json:"livenessProbe,omitempty"`
Lifecycle *v1.Lifecycle `json:"lifecycle,omitempty"`
ServiceMetadata extraMetadata `json:"serviceMetadata,omitempty"`
DeploymentMetadata extraMetadata `json:"deploymentMetadata,omitempty"`
// ServiceMetadata contains Labels and Annotations to be added to a k8s Service of this process.
ServiceMetadata extraMetadata `json:"serviceMetadata,omitempty"`
// DeploymentMetadata contains Labels and Annotations to be added to a k8s Deployment of this process.
DeploymentMetadata extraMetadata `json:"deploymentMetadata,omitempty"`
// PodMetadata contains Labels and Annotations to be added to a k8s Pod of this process.
PodMetadata extraMetadata `json:"podMetadata,omitempty"`
}

type extraMetadata struct {
Expand Down Expand Up @@ -91,43 +91,43 @@ func withPortsAndProbes(c portConfigurator) processOption {
return err
}
p.PublicServicePort = p.ServicePorts[0].Port
p.PodExtra.LivenessProbe = probes.Liveness
p.PodExtra.ReadinessProbe = probes.Readiness
p.LivenessProbe = probes.Liveness
p.ReadinessProbe = probes.Readiness
return nil
}
}

func withSecurityContext(securityContext *v1.SecurityContext) processOption {
return func(p *process) error {
p.PodExtra.SecurityContext = securityContext
p.SecurityContext = securityContext
return nil
}
}

func withLifecycle(lc *v1.Lifecycle) processOption {
return func(p *process) error {
p.PodExtra.Lifecycle = lc
p.Lifecycle = lc
return nil
}
}

func withResourceRequirements(rr *v1.ResourceRequirements) processOption {
return func(p *process) error {
p.PodExtra.ResourceRequirements = rr
p.ResourceRequirements = rr
return nil
}
}

func withVolumes(volumes []v1.Volume) processOption {
return func(p *process) error {
p.PodExtra.Volumes = volumes
p.Volumes = volumes
return nil
}
}

func withVolumeMounts(vm []v1.VolumeMount) processOption {
return func(p *process) error {
p.PodExtra.VolumeMounts = vm
p.VolumeMounts = vm
return nil
}
}
Expand All @@ -144,15 +144,20 @@ func withLabels(labels []ketchv1.MetadataItem, deploymentVersion ketchv1.Deploym
}
for k, v := range label.Apply {
if label.Target.IsDeployment() {
if p.PodExtra.DeploymentMetadata.Labels == nil {
p.PodExtra.DeploymentMetadata.Labels = make(map[string]string)
if p.DeploymentMetadata.Labels == nil {
p.DeploymentMetadata.Labels = make(map[string]string)
}
p.PodExtra.DeploymentMetadata.Labels[k] = v
p.DeploymentMetadata.Labels[k] = v
} else if label.Target.IsService() {
if p.PodExtra.ServiceMetadata.Labels == nil {
p.PodExtra.ServiceMetadata.Labels = make(map[string]string)
if p.ServiceMetadata.Labels == nil {
p.ServiceMetadata.Labels = make(map[string]string)
}
p.PodExtra.ServiceMetadata.Labels[k] = v
p.ServiceMetadata.Labels[k] = v
} else if label.Target.IsPod() {
if p.PodMetadata.Labels == nil {
p.PodMetadata.Labels = make(map[string]string)
}
p.PodMetadata.Labels[k] = v
}
}
}
Expand All @@ -172,15 +177,20 @@ func withAnnotations(annotations []ketchv1.MetadataItem, deploymentVersion ketch
}
for k, v := range annotation.Apply {
if annotation.Target.IsDeployment() {
if p.PodExtra.DeploymentMetadata.Annotations == nil {
p.PodExtra.DeploymentMetadata.Annotations = make(map[string]string)
if p.DeploymentMetadata.Annotations == nil {
p.DeploymentMetadata.Annotations = make(map[string]string)
}
p.PodExtra.DeploymentMetadata.Annotations[k] = v
p.DeploymentMetadata.Annotations[k] = v
} else if annotation.Target.IsService() {
if p.PodExtra.ServiceMetadata.Annotations == nil {
p.PodExtra.ServiceMetadata.Annotations = make(map[string]string)
if p.ServiceMetadata.Annotations == nil {
p.ServiceMetadata.Annotations = make(map[string]string)
}
p.ServiceMetadata.Annotations[k] = v
} else if annotation.Target.IsPod() {
if p.PodMetadata.Annotations == nil {
p.PodMetadata.Annotations = make(map[string]string)
}
p.PodExtra.ServiceMetadata.Annotations[k] = v
p.PodMetadata.Annotations[k] = v
}
}
}
Expand Down
Loading

0 comments on commit 205c450

Please sign in to comment.