Skip to content

Commit

Permalink
cleanup: removes uneeded annotation when not using network policy
Browse files Browse the repository at this point in the history
#### What type of PR is this?

<!--
Add one of the following kinds:
/kind bug
/kind documentation
/kind feature
-->

/kind cleanup

#### What this PR does / why we need it:

Removes the network policy annotation which is not needed (we are not
generating network policy)

#### Which issue(s) this PR fixes:
<!--
*Automatically closes linked issue when PR is merged.
Usage: `Fixes #<issue number>`, or `Fixes (paste link of issue)`.
-->
Fixes #1759

#### Special notes for your reviewer:
  • Loading branch information
cdrage committed May 29, 2024
1 parent 45467ad commit de902d5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
19 changes: 16 additions & 3 deletions pkg/transformer/kubernetes/k8sutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,13 +503,20 @@ func (k *Kubernetes) CreateHeadlessService(name string, service kobject.ServiceC
}

// UpdateKubernetesObjectsMultipleContainers method updates the kubernetes objects with the necessary data
func (k *Kubernetes) UpdateKubernetesObjectsMultipleContainers(name string, service kobject.ServiceConfig, objects *[]runtime.Object, podSpec PodSpec) error {
func (k *Kubernetes) UpdateKubernetesObjectsMultipleContainers(name string, service kobject.ServiceConfig, objects *[]runtime.Object, podSpec PodSpec, opt kobject.ConvertOptions) error {
// Configure annotations
annotations := transformer.ConfigAnnotations(service)

// fillTemplate fills the pod template with the value calculated from config
fillTemplate := func(template *api.PodTemplateSpec) error {
template.ObjectMeta.Labels = transformer.ConfigLabelsWithNetwork(name, service.Network)

// We will ONLY add config labels with network if we actually
// passed in --generate-network-policies to the kompose command
if opt.GenerateNetworkPolicies {
template.ObjectMeta.Labels = transformer.ConfigLabelsWithNetwork(name, service.Network)
} else {
template.ObjectMeta.Labels = transformer.ConfigLabels(name)
}
template.Spec = podSpec.Get()
return nil
}
Expand Down Expand Up @@ -660,7 +667,13 @@ func (k *Kubernetes) UpdateKubernetesObjects(name string, service kobject.Servic
template.Spec.SecurityContext = podSecurityContext
}
template.Spec.Containers[0].Ports = ports
template.ObjectMeta.Labels = transformer.ConfigLabelsWithNetwork(name, service.Network)

// Only add network mode if generate-network-policies is set
if opt.GenerateNetworkPolicies {
template.ObjectMeta.Labels = transformer.ConfigLabelsWithNetwork(name, service.Network)
} else {
template.ObjectMeta.Labels = transformer.ConfigLabels(name)
}

// Configure the image pull policy
policy, err := GetImagePullPolicy(name, service.ImagePullPolicy)
Expand Down
2 changes: 1 addition & 1 deletion pkg/transformer/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,7 @@ func (k *Kubernetes) Transform(komposeObject kobject.KomposeObject, opt kobject.
podSpec.Append(ServiceAccountName(serviceAccountName))
}

err = k.UpdateKubernetesObjectsMultipleContainers(groupName, service, &objects, podSpec)
err = k.UpdateKubernetesObjectsMultipleContainers(groupName, service, &objects, podSpec, opt)
if err != nil {
return nil, errors.Wrap(err, "Error transforming Kubernetes objects")
}
Expand Down
1 change: 0 additions & 1 deletion pkg/transformer/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func newServiceConfig() kobject.ServiceConfig {
WorkingDir: "dir",
Args: []string{"arg1", "arg2"},
VolList: []string{"/tmp/volume"},
Network: []string{"network1", "network2"}, // supported
Labels: nil,
FsGroup: 1001,
Annotations: map[string]string{"abc": "def"},
Expand Down

0 comments on commit de902d5

Please sign in to comment.