Skip to content
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

Add template labels support to kubernetes labels #2473

Open
wants to merge 7 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
93 changes: 90 additions & 3 deletions kubernetes/resource_kubernetes_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,16 @@ func resourceKubernetesLabels() *schema.Resource {
},
},
"labels": {
Type: schema.TypeMap,
Description: "A map of labels to apply to the resource.",
Required: true,
Type: schema.TypeMap,
Description: "A map of labels to apply to the resource.",
Optional: true,
AtLeastOneOf: []string{"template_labels", "labels"},
},
"template_labels": {
Type: schema.TypeMap,
Description: "A map of labels to apply to the resource template.",
Optional: true,
AtLeastOneOf: []string{"template_labels", "labels"},
},
"force": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -165,6 +172,30 @@ func resourceKubernetesLabelsRead(ctx context.Context, d *schema.ResourceData, m
}

d.Set("labels", labels)

kind := d.Get("kind").(string)
configuredTemplateLabels := d.Get("template_labels").(map[string]interface{})
managedTemplateLabels, err := getTemplateManagedLabels(res.GetManagedFields(), fieldManagerName, kind)
if err != nil {
return diag.FromErr(err)
}
var templateLabels map[string]string
if kind == "CronJob" {
templateLabels, _, err = unstructured.NestedStringMap(res.Object, "spec", "jobTemplate", "spec", "template", "metadata", "labels")
} else {
templateLabels, _, err = unstructured.NestedStringMap(res.Object, "spec", "template", "metadata", "labels")
}
if err != nil {
return diag.FromErr(err)
}
for k := range templateLabels {
_, managed := managedTemplateLabels["f:"+k]
_, configured := configuredTemplateLabels[k]
if !managed && !configured {
delete(templateLabels, k)
}
}
d.Set("template_labels", templateLabels)
return nil
}

Expand All @@ -190,6 +221,42 @@ func getManagedLabels(managedFields []v1.ManagedFieldsEntry, manager string) (ma
return labels, nil
}

// getTemplateManagedLabels reads the field manager metadata to discover which fields we're managing
func getTemplateManagedLabels(managedFields []v1.ManagedFieldsEntry, manager string, kind string) (map[string]interface{}, error) {
var labels map[string]interface{}
for _, m := range managedFields {
if m.Manager != manager {
continue
}
var mm map[string]interface{}
err := json.Unmarshal(m.FieldsV1.Raw, &mm)
if err != nil {
return nil, err
}
var spec map[string]interface{}
if s, ok := mm["f:spec"].(map[string]interface{}); ok {
spec = s
}
if kind == "CronJob" {
if jt, ok := spec["f:jobTemplate"].(map[string]interface{}); ok {
spec = jt["f:spec"].(map[string]interface{})
}
}
var template map[string]interface{}
if t, ok := spec["f:template"].(map[string]interface{}); ok {
template = t
}
var metadata map[string]interface{}
if mmm, ok := template["f:metadata"].(map[string]interface{}); ok {
metadata = mmm
}
if l, ok := metadata["f:labels"].(map[string]interface{}); ok {
labels = l
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since labels is declared outside the for loop, this will overwrite it every time there is a match on manager. Should this rather be an append-type operation, adding the contents of l to labels?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since labels is declared outside the for loop, this will overwrite it every time there is a match on manager. Should this rather be an append-type operation, adding the contents of l to labels?

I have directly copied from template_annotations for this part, I will check for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since labels is declared outside the for loop, this will overwrite it every time there is a match on manager. Should this rather be an append-type operation, adding the contents of l to labels?

Hi @alexsomesan yeah, I agree there could be potential issues in theory, however, I wasn't able to reproduce it with kubectl.

I tried kubectl apply and kubectl patch on same object multiple times, however, looks like the server will store different manager kubectl-client-side-apply and kubectl-patch.

Do you have any suggestions on reproducing this issue? Or you guys are fine for me to change the code (without testing this case).

Copy link
Contributor

@sheneska sheneska May 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @theloneexplorerquest, this issue is specific to using server-side-apply to access the API, however it appears you may be using client-side-apply as per the name of manager. kubectl by default uses client-side-apply and requires you to explicitly state --server-side in order to use the server-side-apply. Here's a useful link.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @theloneexplorerquest, this issue is specific to using server-side-apply to access the API, however it appears you may be using client-side-apply as per the name of manager. kubectl by default uses client-side-apply and requires you to explicitly state --server-side in order to use the server-side-apply. Here's a useful link.

Hi @sheneska thanks for reply. Yeah, I actually tried --server-side, however, I wasn't able to generate manifest with two field managers both named kubectl, do you have suggestion for that?

}
}
return labels, nil
}

func resourceKubernetesLabelsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
conn, err := m.(KubeClientsets).DynamicClient()
if err != nil {
Expand Down Expand Up @@ -247,10 +314,12 @@ func resourceKubernetesLabelsUpdate(ctx context.Context, d *schema.ResourceData,

// craft the patch to update the labels
labels := d.Get("labels")
templateLabels := d.Get("template_labels")
if d.Id() == "" {
// if we're deleting then just we just patch
// with an empty labels map
labels = map[string]interface{}{}
templateLabels = map[string]interface{}{}
}

patchmeta := map[string]interface{}{
Expand All @@ -265,6 +334,24 @@ func resourceKubernetesLabelsUpdate(ctx context.Context, d *schema.ResourceData,
"kind": kind,
"metadata": patchmeta,
}
if _, ok := d.GetOk("template_labels"); ok {
spec := map[string]interface{}{
"template": map[string]interface{}{
"metadata": map[string]interface{}{
"labels": templateLabels,
},
},
}
if kind == "CronJob" {
patchobj["spec"] = map[string]interface{}{
"jobTemplate": map[string]interface{}{
"spec": spec,
},
}
} else {
patchobj["spec"] = spec
}
}
patch := unstructured.Unstructured{}
patch.Object = patchobj
patchbytes, err := patch.MarshalJSON()
Expand Down
Loading
Loading