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

Fix tinkerbell IP in hegel urls controller #7574

Merged
merged 3 commits into from
Feb 15, 2024
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
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) {
Copy link
Member

Choose a reason for hiding this comment

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

You might consider not using the bool in the return. The alternative is for consumers to check if the returned string is empty or not. The bool adds extra checking that doesn't feel necessary. If you do go with this idea, you might then rethink the name of the method. Maybe just remove the Has, so its just TinkerbellIPAnnotation.

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)
Copy link
Member

Choose a reason for hiding this comment

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

a code comment here are why we need to add this annotation will be very helpful for future us.

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 {
Copy link
Member

Choose a reason for hiding this comment

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

a code comment here around why this is the only place we remove the annotation would be very helpful to future us.

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
Loading