Skip to content

Commit

Permalink
Use tag helpers from metal-lib. (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jul 31, 2023
1 parent 788c7a9 commit b1c07f8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 38 deletions.
4 changes: 2 additions & 2 deletions pkg/controllers/housekeeping/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/klog/v2"

"github.com/metal-stack/metal-ccm/pkg/resources/kubernetes"
ccmtags "github.com/metal-stack/metal-ccm/pkg/tags"
"github.com/metal-stack/metal-lib/pkg/tag"
metaltag "github.com/metal-stack/metal-lib/pkg/tag"
)

Expand Down Expand Up @@ -62,7 +62,7 @@ func (h *Housekeeper) syncMachineTagsToNodeLabels() error {
}

// check if machine has a cluster tag, if not add it
if machineClusterTag, found := ccmtags.GetMachineClusterTag(tags); !found || machineClusterTag != h.clusterID {
if machineClusterTag, found := tag.NewTagMap(tags).Value(tag.ClusterID); !found || machineClusterTag != h.clusterID {
m, err := h.ms.GetMachineFromNode(context.Background(), &n)

if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions pkg/controllers/loadbalancer/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,13 @@ func (l *LoadBalancerController) UpdateMetalLBConfig(ctx context.Context, nodes
}

func (l *LoadBalancerController) useIPInCluster(ctx context.Context, ip models.V1IPResponse, clusterID string, s v1.Service) (*models.V1IPResponse, error) {
for _, t := range ip.Tags {
if tags.IsMachine(t) {
return nil, fmt.Errorf("ip is used for a machine, can not use it for a service, ip tags: %v", ip.Tags)
}
if tags.IsEgress(t) {
return nil, fmt.Errorf("ip is used for egress purposes, can not use it for a service, ip tags: %v", ip.Tags)
}
tm := tag.NewTagMap(ip.Tags)

if _, ok := tm.Value(tag.MachineID); ok {
return nil, fmt.Errorf("ip is used for a machine, can not use it for a service, ip tags: %v", ip.Tags)
}
if _, ok := tm.Value(tag.ClusterEgress); ok {
return nil, fmt.Errorf("ip is used for egress purposes, can not use it for a service, ip tags: %v", ip.Tags)
}

serviceTag := tags.BuildClusterServiceFQNTag(clusterID, s.GetNamespace(), s.GetName())
Expand Down
16 changes: 10 additions & 6 deletions pkg/resources/metal/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/metal-stack/metal-ccm/pkg/tags"

metalip "github.com/metal-stack/metal-go/api/client/ip"
"github.com/metal-stack/metal-lib/pkg/tag"

"github.com/metal-stack/metal-go/api/models"
v1 "k8s.io/api/core/v1"
Expand All @@ -28,13 +29,16 @@ func (ms *MetalService) FindClusterIPs(ctx context.Context, projectID, clusterID

result := []*models.V1IPResponse{}
for _, i := range resp.Payload {
tm := tag.NewTagMap(i.Tags)

if _, ok := tm.Value(tag.ClusterEgress); ok {
continue
}
if _, ok := tm.Value(tag.MachineID); ok {
continue
}

for _, t := range i.Tags {
if tags.IsEgress(t) {
continue
}
if tags.IsMachine(t) {
continue
}
if tags.IsMemberOfCluster(t, clusterID) {
result = append(result, i)
break
Expand Down
23 changes: 0 additions & 23 deletions pkg/tags/tagUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ func BuildClusterServiceFQNTag(clusterID string, namespace, serviceName string)
return fmt.Sprintf("%s=%s/%s/%s", t.ClusterServiceFQN, clusterID, namespace, serviceName)
}

// IsMachine returns true if the given tag is a machine-tag.
func IsMachine(tag string) bool {
return strings.HasPrefix(tag, t.MachineID)
}

// IsEgress returns true if the given tag is an egress-tag
func IsEgress(tag string) bool {
return strings.HasPrefix(tag, t.ClusterEgress)
}

// IsMemberOfCluster returns true of the given tag is a cluster-tag and clusterID matches.
// tag is in the form of:
//
Expand All @@ -38,16 +28,3 @@ func IsMemberOfCluster(tag, clusterID string) bool {
}
return false
}

// GetMachineClusterTag returns the clusterID in the given tags.
func GetMachineClusterTag(tags []string) (string, bool) {
found := false
value := ""
for _, tag := range tags {
if strings.HasPrefix(tag, t.ClusterID) {
_, value, found = strings.Cut(tag, "=")
break
}
}
return value, found
}

0 comments on commit b1c07f8

Please sign in to comment.