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

Feat: cleanup ig resources #51

Closed
wants to merge 8 commits into from
190 changes: 114 additions & 76 deletions internal/controller/ec2.aws/securitygroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ func (c *SecurityGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return resultError, client.IgnoreNotFound(err)
}

if r.sg.Spec.InfrastructureRef == nil {
return resultDefault, fmt.Errorf("infrastructureRef not supported")
}

if r.sg.GetAnnotations()[AnnotationKeyReconciliationPaused] == "true" {
r.log.Info("Reconciliation is paused via the pause annotation", "annotation", AnnotationKeyReconciliationPaused, "value", "true")
r.Recorder.Eventf(r.sg, corev1.EventTypeNormal, securitygroupv1alpha2.ReasonReconcilePaused, "Reconciliation is paused via the pause annotation")
Expand All @@ -136,111 +132,153 @@ func (c *SecurityGroupReconciler) Reconcile(ctx context.Context, req ctrl.Reques
r.log.Info(fmt.Sprintf("starting reconcile loop for %s", r.sg.ObjectMeta.GetName()))

defer func() {
if err := r.Status().Update(ctx, r.sg); err != nil {
r.Recorder.Eventf(r.sg, corev1.EventTypeWarning, "FailedToUpdateStatus", "failed to update security group status %s: %s", r.sg.Name, err)
securityGroupHelper := r.sg.DeepCopy()
if err := r.Update(ctx, r.sg); err != nil {
r.Recorder.Eventf(r.sg, corev1.EventTypeWarning, "FailedToUpdate", "failed to update security group %s: %s", r.sg.Name, err)
if rerr == nil {
rerr = err
}
}

if securityGroupHelper.ObjectMeta.DeletionTimestamp.IsZero() {
r.sg.Status = securityGroupHelper.Status
if err := r.Status().Update(ctx, r.sg); err != nil {
r.Recorder.Eventf(r.sg, corev1.EventTypeWarning, "FailedToUpdateStatus", "failed to update security group status %s: %s", r.sg.Name, err)
if rerr == nil {
rerr = err
}
}
}

r.log.Info(fmt.Sprintf("finished reconcile loop for %s", r.sg.ObjectMeta.GetName()))
}()

existingInfrastructureRef := []*corev1.ObjectReference{}

for _, infrastructureRef := range r.sg.Spec.InfrastructureRef {
switch infrastructureRef.Kind {
case "KopsMachinePool":
kmp := kinfrastructurev1alpha1.KopsMachinePool{}
key := client.ObjectKey{
Name: infrastructureRef.Name,
Namespace: infrastructureRef.Namespace,
}
if err := r.Client.Get(ctx, key, &kmp); err == nil {
existingInfrastructureRef = append(existingInfrastructureRef, infrastructureRef)
}
case "KopsControlPlane":
kcp := &kcontrolplanev1alpha1.KopsControlPlane{}
key := client.ObjectKey{
Name: infrastructureRef.Name,
Namespace: infrastructureRef.Namespace,
}
err := r.Client.Get(ctx, key, kcp)
if err == nil {
existingInfrastructureRef = append(existingInfrastructureRef, infrastructureRef)
}
default:
existingInfrastructureRef = append(existingInfrastructureRef, infrastructureRef)
}
}
r.sg.Spec.InfrastructureRef = existingInfrastructureRef

err := r.retrieveInfraRefInfo(ctx)
if err != nil {
return resultError, err
}

if !r.sg.ObjectMeta.DeletionTimestamp.IsZero() {
if len(r.sg.Spec.InfrastructureRef) == 0 || !r.sg.ObjectMeta.DeletionTimestamp.IsZero() {
return r.reconcileDelete(ctx, r.sg)
}

return r.reconcileNormal(ctx)
}

func (r *SecurityGroupReconciliation) retrieveInfraRefInfo(ctx context.Context) error {
if len(r.sg.Spec.InfrastructureRef) == 0 {
return fmt.Errorf("no infrastructureRef found")
}

switch r.sg.Spec.InfrastructureRef[0].Kind {
case "KopsMachinePool":
kmp := kinfrastructurev1alpha1.KopsMachinePool{}
key := client.ObjectKey{
Name: r.sg.Spec.InfrastructureRef[0].Name,
Namespace: r.sg.Spec.InfrastructureRef[0].Namespace,
}
if err := r.Client.Get(ctx, key, &kmp); err != nil {
return err
}
/* Only need to use the first infrastructureRef element cause SecurityGroups are region and VPC bounded,
no need to iterate over all, they all belong to the same region and VPC.*/
if len(r.sg.Spec.InfrastructureRef) != 0 {
switch r.sg.Spec.InfrastructureRef[0].Kind {
case "KopsMachinePool":
kmp := kinfrastructurev1alpha1.KopsMachinePool{}
key := client.ObjectKey{
Name: r.sg.Spec.InfrastructureRef[0].Name,
Namespace: r.sg.Spec.InfrastructureRef[0].Namespace,
}
if err := r.Client.Get(ctx, key, &kmp); err != nil {
return err
}

kcp := &kcontrolplanev1alpha1.KopsControlPlane{}
key = client.ObjectKey{
Name: kmp.Spec.ClusterName,
Namespace: kmp.ObjectMeta.Namespace,
}
if err := r.Client.Get(ctx, key, kcp); err != nil {
return err
}
kcp := &kcontrolplanev1alpha1.KopsControlPlane{}
key = client.ObjectKey{
Name: kmp.Spec.ClusterName,
Namespace: kmp.ObjectMeta.Namespace,
}
if err := r.Client.Get(ctx, key, kcp); err != nil {
return err
}

region, err := kopsutils.GetRegionFromKopsControlPlane(ctx, kcp)
if err != nil {
return fmt.Errorf("error retrieving region: %w", err)
}
region, err := kopsutils.GetRegionFromKopsControlPlane(ctx, kcp)
if err != nil {
return fmt.Errorf("error retrieving region: %w", err)
}

providerConfigName, awsCfg, err := kopsutils.RetrieveAWSCredentialsFromKCP(ctx, r.Client, region, kcp)
if err != nil {
return err
}
providerConfigName, awsCfg, err := kopsutils.RetrieveAWSCredentialsFromKCP(ctx, r.Client, region, kcp)
if err != nil {
return err
}

r.ec2Client = r.NewEC2ClientFactory(*awsCfg)
r.asgClient = r.NewAutoScalingClientFactory(*awsCfg)
r.ec2Client = r.NewEC2ClientFactory(*awsCfg)
r.asgClient = r.NewAutoScalingClientFactory(*awsCfg)

vpcId, err := ec2.GetVPCIdWithCIDRAndClusterName(ctx, r.ec2Client, kcp.Name, kcp.Spec.KopsClusterSpec.Networking.NetworkCIDR)
if err != nil {
return fmt.Errorf("error retrieving vpcID: %w", err)
}
vpcId, err := ec2.GetVPCIdWithCIDRAndClusterName(ctx, r.ec2Client, kcp.Name, kcp.Spec.KopsClusterSpec.Networking.NetworkCIDR)
if err != nil {
return fmt.Errorf("error retrieving vpcID: %w", err)
}

r.providerConfigName = providerConfigName
r.region = region
r.vpcId = vpcId
r.providerConfigName = providerConfigName
r.region = region
r.vpcId = vpcId

return nil
case "KopsControlPlane":
kcp := &kcontrolplanev1alpha1.KopsControlPlane{}
key := client.ObjectKey{
Name: r.sg.Spec.InfrastructureRef[0].Name,
Namespace: r.sg.Spec.InfrastructureRef[0].Namespace,
}
if err := r.Client.Get(ctx, key, kcp); err != nil {
return err
}
return nil
case "KopsControlPlane":
kcp := &kcontrolplanev1alpha1.KopsControlPlane{}
key := client.ObjectKey{
Name: r.sg.Spec.InfrastructureRef[0].Name,
Namespace: r.sg.Spec.InfrastructureRef[0].Namespace,
}
if err := r.Client.Get(ctx, key, kcp); err != nil {
return err
}

region, err := kopsutils.GetRegionFromKopsControlPlane(ctx, kcp)
if err != nil {
return fmt.Errorf("error retrieving region: %w", err)
}
region, err := kopsutils.GetRegionFromKopsControlPlane(ctx, kcp)
if err != nil {
return fmt.Errorf("error retrieving region: %w", err)
}

providerConfigName, awsCfg, err := kopsutils.RetrieveAWSCredentialsFromKCP(ctx, r.Client, region, kcp)
if err != nil {
return err
}
providerConfigName, awsCfg, err := kopsutils.RetrieveAWSCredentialsFromKCP(ctx, r.Client, region, kcp)
if err != nil {
return err
}

r.ec2Client = r.NewEC2ClientFactory(*awsCfg)
r.asgClient = r.NewAutoScalingClientFactory(*awsCfg)
r.ec2Client = r.NewEC2ClientFactory(*awsCfg)
r.asgClient = r.NewAutoScalingClientFactory(*awsCfg)

vpcId, err := ec2.GetVPCIdWithCIDRAndClusterName(ctx, r.ec2Client, kcp.Name, kcp.Spec.KopsClusterSpec.Networking.NetworkCIDR)
if err != nil {
return fmt.Errorf("error retrieving vpcID: %w", err)
}
vpcId, err := ec2.GetVPCIdWithCIDRAndClusterName(ctx, r.ec2Client, kcp.Name, kcp.Spec.KopsClusterSpec.Networking.NetworkCIDR)
if err != nil {
return fmt.Errorf("error retrieving vpcID: %w", err)
}

r.providerConfigName = providerConfigName
r.region = region
r.vpcId = vpcId
r.providerConfigName = providerConfigName
r.region = region
r.vpcId = vpcId

return nil
default:
return fmt.Errorf("infrastructureRef not supported")
return nil
default:
return fmt.Errorf("infrastructureRef not supported")
}
}
return nil
}

func (r *SecurityGroupReconciliation) reconcileNormal(ctx context.Context) (ctrl.Result, error) {
Expand Down
Loading
Loading