Skip to content

Commit

Permalink
Fix tinkerbell IP in hegel urls controller (#7574)
Browse files Browse the repository at this point in the history
* Fix tinkerbell IP in hegel urls controller

* add test

* unit test
  • Loading branch information
mitalipaygude authored Feb 15, 2024
1 parent a915a47 commit 44d1394
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/api/v1alpha1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,29 @@ func (c *Cluster) ClearManagedByCLIAnnotation() {
}
}

// AddTinkerbellIPAnnotation adds the managed-by-cli annotation to the cluster.
func (c *Cluster) AddTinkerbellIPAnnotation(tinkerbellIP string) {
if c.Annotations == nil {
c.Annotations = map[string]string{}
}
c.Annotations[tinkerbellIPAnnotation] = tinkerbellIP
}

// ClearTinkerbellIPAnnotation removes the managed-by-cli annotation from the cluster.
func (c *Cluster) ClearTinkerbellIPAnnotation() {
if c.Annotations != nil {
delete(c.Annotations, tinkerbellIPAnnotation)
}
}

// HasTinkerbellIPAnnotation returns the tinkerbell IP value if the annotation exists.
func (c *Cluster) HasTinkerbellIPAnnotation() (string, bool) {
if tinkerbellIP, ok := c.Annotations[tinkerbellIPAnnotation]; ok {
return tinkerbellIP, true
}
return "", false
}

// RegistryAuth returns whether registry requires authentication or not.
func (c *Cluster) RegistryAuth() bool {
if c.Spec.RegistryMirrorConfiguration == nil {
Expand Down
18 changes: 18 additions & 0 deletions pkg/api/v1alpha1/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,24 @@ func TestCluster_AddRemoveManagedByCLIAnnotation(t *testing.T) {
g.Expect(ok).To(BeFalse())
}

func TestClusterClearTinkerbellIPAnnotation(t *testing.T) {
g := NewWithT(t)
c := &Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster_test",
},
}
c.AddTinkerbellIPAnnotation("1.1.1.1")
val, ok := c.HasTinkerbellIPAnnotation()

g.Expect(ok).To(BeTrue())
g.Expect(val).To(ContainSubstring("1.1.1.1"))

c.ClearTinkerbellIPAnnotation()
_, ok = c.Annotations[tinkerbellIPAnnotation]
g.Expect(ok).To(BeFalse())
}

func TestGitOpsEquals(t *testing.T) {
tests := []struct {
name string
Expand Down
4 changes: 4 additions & 0 deletions pkg/api/v1alpha1/cluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const (
// the controller will remove the finalizer and let the cluster be deleted.
ManagedByCLIAnnotation = "anywhere.eks.amazonaws.com/managed-by-cli"

// tinkerbellIPAnnotation can be applied to an EKS-A Cluster to convey the tinkerbell bootstrap ip to the
// EKSA controller. When marked for deletion, the controller will remove the IP annotation.
tinkerbellIPAnnotation = "anywhere.eks.amazonaws.com/tinkerbell-bootstrap-ip"

// ControlPlaneAnnotation is an annotation that can be applied to EKS-A machineconfig
// object to prevent a controller from making changes to that resource.
controlPlaneAnnotation = "anywhere.eks.amazonaws.com/control-plane"
Expand Down
2 changes: 2 additions & 0 deletions pkg/providers/tinkerbell/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func (p *Provider) BootstrapClusterOpts(_ *cluster.Spec) ([]bootstrapper.Bootstr
func (p *Provider) PreCAPIInstallOnBootstrap(ctx context.Context, cluster *types.Cluster, clusterSpec *cluster.Spec) error {
logger.V(4).Info("Installing Tinkerbell stack on bootstrap cluster")

logger.V(4).Info("Adding annotation for tinkerbell ip on bootstrap cluster")
clusterSpec.Cluster.AddTinkerbellIPAnnotation(p.tinkerbellIP)
versionsBundle := clusterSpec.RootVersionsBundle()

err := p.stackInstaller.Install(
Expand Down
8 changes: 8 additions & 0 deletions pkg/providers/tinkerbell/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (tb *TemplateBuilder) GenerateCAPISpecControlPlane(clusterSpec *cluster.Spe
}
var OSImageURL string

if tinkerbellIP, ok := clusterSpec.Cluster.HasTinkerbellIPAnnotation(); ok {
tb.tinkerbellIP = tinkerbellIP
}

if cpTemplateConfig == nil {
OSImageURL = clusterSpec.TinkerbellDatacenter.Spec.OSImageURL
if tb.controlPlaneMachineSpec.OSImageURL != "" {
Expand Down Expand Up @@ -123,6 +127,10 @@ func (tb *TemplateBuilder) GenerateCAPISpecWorkers(clusterSpec *cluster.Spec, wo
bundle := clusterSpec.RootVersionsBundle()
OSImageURL := clusterSpec.TinkerbellDatacenter.Spec.OSImageURL

if tinkerbellIP, ok := clusterSpec.Cluster.HasTinkerbellIPAnnotation(); ok {
tb.tinkerbellIP = tinkerbellIP
}

for _, workerNodeGroupConfiguration := range clusterSpec.Cluster.Spec.WorkerNodeGroupConfigurations {
workerNodeMachineSpec := tb.WorkerNodeGroupMachineSpecs[workerNodeGroupConfiguration.MachineGroupRef.Name]
wTemplateConfig := clusterSpec.TinkerbellTemplateConfigs[workerNodeMachineSpec.TemplateRef.Name]
Expand Down
31 changes: 31 additions & 0 deletions pkg/providers/tinkerbell/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,34 @@ func TestTemplateBuilder_CertSANs(t *testing.T) {

}
}

func TestTemplateBuilder(t *testing.T) {
for _, tc := range []struct {
Input string
Output string
}{
{
Input: "testdata/cluster_tinkerbell_api_server_cert_san_ip.yaml",
Output: "testdata/expected_cluster_tinkerbell_api_server_cert_san_ip.yaml",
},
} {
g := NewWithT(t)
clusterSpec := test.NewFullClusterSpec(t, tc.Input)
clusterSpec.Cluster.AddTinkerbellIPAnnotation("1.1.1.1")

cpMachineCfg, err := getControlPlaneMachineSpec(clusterSpec)
g.Expect(err).ToNot(HaveOccurred())

wngMachineCfgs, err := getWorkerNodeGroupMachineSpec(clusterSpec)
g.Expect(err).ToNot(HaveOccurred())

tinkIPBefore := "0.0.0.0"
bldr := NewTemplateBuilder(&clusterSpec.TinkerbellDatacenter.Spec, cpMachineCfg, nil, wngMachineCfgs, tinkIPBefore, time.Now)

data, err := bldr.GenerateCAPISpecControlPlane(clusterSpec)
g.Expect(err).ToNot(HaveOccurred())

test.AssertContentToFile(t, string(data), tc.Output)

}
}
6 changes: 6 additions & 0 deletions pkg/workflows/management/create_install_eksa.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package management
import (
"context"

"github.com/aws/eks-anywhere/pkg/api/v1alpha1"
"github.com/aws/eks-anywhere/pkg/cluster"
"github.com/aws/eks-anywhere/pkg/logger"
"github.com/aws/eks-anywhere/pkg/task"
Expand Down Expand Up @@ -38,6 +39,11 @@ func (s *installEksaComponentsOnBootstrapTask) Checkpoint() *task.CompletedTask
type installEksaComponentsOnWorkloadTask struct{}

func (s *installEksaComponentsOnWorkloadTask) Run(ctx context.Context, commandContext *task.CommandContext) task.Task {
if commandContext.ClusterSpec.Cluster.Spec.DatacenterRef.Kind == v1alpha1.TinkerbellDatacenterKind {
logger.Info("Removing Tinkerbell IP annotation")
commandContext.ClusterSpec.Cluster.ClearTinkerbellIPAnnotation()
}

logger.Info("Installing EKS-A custom components on workload cluster")

err := installEKSAComponents(ctx, commandContext, commandContext.WorkloadCluster)
Expand Down

0 comments on commit 44d1394

Please sign in to comment.