Skip to content

feat: add GKE instance template metadata resolver to remove conflict label in karpenter. #34

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

Merged
merged 1 commit into from
Apr 30, 2025
Merged
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
89 changes: 89 additions & 0 deletions pkg/providers/metadata/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2025 The CloudPilot AI Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metadata

import (
"context"
"errors"
"fmt"
"strings"

"github.com/go-openapi/swag"
"github.com/samber/lo"
"google.golang.org/api/compute/v1"

"github.com/cloudpilot-ai/karpenter-provider-gcp/pkg/providers/nodepooltemplate"
)

const (
ClusterNameLabel = "cluster-name"
GKENodePoolLabel = "cloud.google.com/gke-nodepool"
)

type Metadata struct {
nodePoolTemplateProvider nodepooltemplate.Provider
}

func NewMetadata(nodePoolTemplateProvider nodepooltemplate.Provider) *Metadata {
return &Metadata{
nodePoolTemplateProvider: nodePoolTemplateProvider,
}
}

// ResolveMetadata resolves the metadata for the node pool template
// In default nodepool, gke will create a default nodepool template with default custom metadata.
// We need to make sure the label `cloud.google.com/gke-nodepool=cluster-name` in metadata is removed.
func (m *Metadata) ResolveMetadata(ctx context.Context) (map[string]*compute.Metadata, error) {
instanceTemplates, err := m.nodePoolTemplateProvider.GetInstanceTemplates(ctx)
if err != nil {
return nil, err
}

metadataMap := map[string]*compute.Metadata{}
for _, instanceTemplate := range instanceTemplates {
if err := m.removeGKEBuiltinLabels(instanceTemplate.Properties.Metadata); err != nil {
return nil, err
}
metadataMap[instanceTemplate.Name] = instanceTemplate.Properties.Metadata
}

return metadataMap, nil
}

func (m *Metadata) removeGKEBuiltinLabels(metadata *compute.Metadata) error {
// Get cluster name
clusterNameEntry := lo.Filter(metadata.Items, func(item *compute.MetadataItems, _ int) bool {
return item.Key == ClusterNameLabel
})
if len(clusterNameEntry) != 1 {
return errors.New("cluster name label not found")
}
clusterName := swag.StringValue(clusterNameEntry[0].Value)
nodePoolLabelEntry := fmt.Sprintf("%s=%s", GKENodePoolLabel, clusterName)

// Remove nodePoolLabelEntry from `kube-labels` and `kube-env`
for _, item := range metadata.Items {
if item.Key == "kube-labels" {
item.Value = swag.String(strings.ReplaceAll(swag.StringValue(item.Value), nodePoolLabelEntry, ""))
}
if item.Key == "kube-env" {
item.Value = swag.String(strings.ReplaceAll(swag.StringValue(item.Value), nodePoolLabelEntry, ""))
}
}

return nil
}
2 changes: 1 addition & 1 deletion pkg/providers/nodepooltemplate/nodepooltemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (p *DefaultProvider) Create(ctx context.Context) error {
func (p *DefaultProvider) ensureKarpenterNodePoolTemplate(ctx context.Context, imageType, nodePoolName string) error {
logger := log.FromContext(ctx)

// adding simple validation, becuase previous code was failing
// adding simple validation, because previous code was failing
// here and no reasonable log was printed out
if p.ClusterInfo.Name == "" {
return fmt.Errorf("clusterName is required but was empty")
Expand Down