Skip to content

Commit

Permalink
🌱 Suffixing placement group names in Hetzner
Browse files Browse the repository at this point in the history
Using cluster name as suffix for placement group names to avoid
colliding naming of placement groups belonging to different clusters in
the same project.
  • Loading branch information
janiskemper committed Jan 11, 2022
1 parent c515ccf commit b422e04
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var (
)

func main() {
flag.BoolVar(&verbose, "verbose", true, "Enable verbose logging")
flag.BoolVar(&verbose, "verbose", false, "Enable verbose logging")

flag.StringVar(&metricsAddr, "metrics-bind-address", "localhost:8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
Expand All @@ -73,7 +73,7 @@ func main() {
flag.StringVar(&watchNamespace, "namespace", "", "Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.")

opts := zap.Options{
Development: true,
Development: verbose,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
Expand Down
9 changes: 7 additions & 2 deletions pkg/services/hcloud/placementgroup/placementgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package placementgroup

import (
"context"
"fmt"
"strings"

"github.com/hetznercloud/hcloud-go/hcloud"
"github.com/pkg/errors"
Expand All @@ -35,6 +37,8 @@ type Service struct {
scope *scope.ClusterScope
}

const pgDelimiter = "-"

// NewService creates new service object.
func NewService(scope *scope.ClusterScope) *Service {
return &Service{
Expand Down Expand Up @@ -88,9 +92,10 @@ func (s *Service) Reconcile(ctx context.Context) (err error) {
}
}
if !foundInStatus {
name := fmt.Sprintf("%s%s%s", s.scope.HetznerCluster.Name, pgDelimiter, pgSpec.Name)
clusterTagKey := infrav1.ClusterTagKey(s.scope.HetznerCluster.Name)
if _, _, err := s.scope.HCloudClient().CreatePlacementGroup(ctx, hcloud.PlacementGroupCreateOpts{
Name: pgSpec.Name,
Name: name,
Type: hcloud.PlacementGroupType(pgSpec.Type),
Labels: map[string]string{clusterTagKey: string(infrav1.ResourceLifecycleOwned)},
}); err != nil {
Expand Down Expand Up @@ -154,7 +159,7 @@ func (s *Service) apiToStatus(placementGroups []*hcloud.PlacementGroup) (status
status = append(status, infrav1.HCloudPlacementGroupStatus{
ID: pg.ID,
Server: pg.Servers,
Name: pg.Name,
Name: strings.Split(pg.Name, pgDelimiter)[1],
Type: string(pg.Type),
})
}
Expand Down

0 comments on commit b422e04

Please sign in to comment.