Skip to content

Commit

Permalink
Merge pull request #20 from Leaseweb/small_fixes
Browse files Browse the repository at this point in the history
Remove dead code and other small fixes
  • Loading branch information
hrak authored Jul 12, 2024
2 parents 116b93c + dc8037a commit 655c562
Show file tree
Hide file tree
Showing 12 changed files with 8 additions and 348 deletions.
4 changes: 2 additions & 2 deletions api/v1beta1/cloudstackmachinetemplate_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Convert_v1beta3_CloudStackMachineTemplateSpec_To_v1beta1_CloudStackMachineT
return Convert_v1beta3_CloudStackMachineTemplateResource_To_v1beta1_CloudStackMachineTemplateResource(&in.Template, &out.Spec, s)
}

func Convert_v1beta1_ObjectMeta_To_v1_ObjectMeta(in *clusterv1.ObjectMeta, out *metav1.ObjectMeta, s machineryconversion.Scope) error { // nolint
func Convert_v1beta1_ObjectMeta_To_v1_ObjectMeta(in *clusterv1.ObjectMeta, out *metav1.ObjectMeta, _ machineryconversion.Scope) error { // nolint
if in.Annotations != nil {
out.Annotations = in.Annotations
}
Expand All @@ -74,7 +74,7 @@ func Convert_v1beta1_ObjectMeta_To_v1_ObjectMeta(in *clusterv1.ObjectMeta, out *
return nil
}

func Convert_v1_ObjectMeta_To_v1beta1_ObjectMeta(in *metav1.ObjectMeta, out *clusterv1.ObjectMeta, s machineryconversion.Scope) error { // nolint
func Convert_v1_ObjectMeta_To_v1beta1_ObjectMeta(in *metav1.ObjectMeta, out *clusterv1.ObjectMeta, _ machineryconversion.Scope) error { // nolint
if in.Annotations != nil {
out.Annotations = in.Annotations
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
const DefaultEndpointCredential = "global"

//nolint:golint,revive,stylecheck
func Convert_v1beta1_CloudStackCluster_To_v1beta3_CloudStackCluster(in *CloudStackCluster, out *infrav1.CloudStackCluster, s machineryconversion.Scope) error {
func Convert_v1beta1_CloudStackCluster_To_v1beta3_CloudStackCluster(in *CloudStackCluster, out *infrav1.CloudStackCluster, _ machineryconversion.Scope) error {
out.ObjectMeta = in.ObjectMeta
failureDomains, err := GetFailureDomains(in)
if err != nil {
Expand All @@ -49,7 +49,7 @@ func Convert_v1beta1_CloudStackCluster_To_v1beta3_CloudStackCluster(in *CloudSta
}

//nolint:golint,revive,stylecheck
func Convert_v1beta3_CloudStackCluster_To_v1beta1_CloudStackCluster(in *infrav1.CloudStackCluster, out *CloudStackCluster, s machineryconversion.Scope) error {
func Convert_v1beta3_CloudStackCluster_To_v1beta1_CloudStackCluster(in *infrav1.CloudStackCluster, out *CloudStackCluster, _ machineryconversion.Scope) error {
if len(in.Spec.FailureDomains) < 1 {
return fmt.Errorf("infrav1 to v1beta1 conversion not supported when < 1 failure domain is provided. Input CloudStackCluster spec %s", in.Spec)
}
Expand Down
3 changes: 0 additions & 3 deletions api/v1beta2/cloudstackcluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,12 @@ package v1beta2
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
ClusterFinalizer = "cloudstackcluster.infrastructure.cluster.x-k8s.io"
)

var K8sClient client.Client

// CloudStackClusterSpec defines the desired state of CloudStackCluster.
type CloudStackClusterSpec struct {
FailureDomains []CloudStackFailureDomainSpec `json:"failureDomains"`
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta2/cloudstackmachinetemplate_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Convert_v1beta3_CloudStackMachineTemplateSpec_To_v1beta2_CloudStackMachineT
return Convert_v1beta3_CloudStackMachineTemplateResource_To_v1beta2_CloudStackMachineTemplateResource(&in.Template, &out.Spec, s)
}

func Convert_v1beta1_ObjectMeta_To_v1_ObjectMeta(in *clusterv1.ObjectMeta, out *metav1.ObjectMeta, s machineryconversion.Scope) error { // nolint
func Convert_v1beta1_ObjectMeta_To_v1_ObjectMeta(in *clusterv1.ObjectMeta, out *metav1.ObjectMeta, _ machineryconversion.Scope) error { // nolint
if in.Annotations != nil {
out.Annotations = in.Annotations
}
Expand All @@ -68,7 +68,7 @@ func Convert_v1beta1_ObjectMeta_To_v1_ObjectMeta(in *clusterv1.ObjectMeta, out *
return nil
}

func Convert_v1_ObjectMeta_To_v1beta1_ObjectMeta(in *metav1.ObjectMeta, out *clusterv1.ObjectMeta, s machineryconversion.Scope) error { // nolint
func Convert_v1_ObjectMeta_To_v1beta1_ObjectMeta(in *metav1.ObjectMeta, out *clusterv1.ObjectMeta, _ machineryconversion.Scope) error { // nolint
if in.Annotations != nil {
out.Annotations = in.Annotations
}
Expand Down
92 changes: 0 additions & 92 deletions controllers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"k8s.io/klog/v2"
infrav1 "sigs.k8s.io/cluster-api-provider-cloudstack/api/v1beta3"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
capiControlPlanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1beta1"
"sigs.k8s.io/cluster-api/util"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -41,88 +40,6 @@ import (
)

// getMachineSetFromCAPIMachine attempts to fetch a MachineSet from CAPI machine owner reference.
func getMachineSetFromCAPIMachine(
ctx context.Context,
c client.Client,
capiMachine *clusterv1.Machine,
) (*clusterv1.MachineSet, error) {

ref := GetManagementOwnerRef(capiMachine)
if ref == nil {
return nil, errors.New("management owner not found")
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}
if gv.Group == clusterv1.GroupVersion.Group {
key := client.ObjectKey{
Namespace: capiMachine.Namespace,
Name: ref.Name,
}

machineSet := &clusterv1.MachineSet{}
if err := c.Get(ctx, key, machineSet); err != nil {
return nil, err
}

return machineSet, nil
}
return nil, nil
}

// getKubeadmControlPlaneFromCAPIMachine attempts to fetch a KubeadmControlPlane from a CAPI machine owner reference.
func getKubeadmControlPlaneFromCAPIMachine(
ctx context.Context,
c client.Client,
capiMachine *clusterv1.Machine,
) (*capiControlPlanev1.KubeadmControlPlane, error) {

ref := GetManagementOwnerRef(capiMachine)
if ref == nil {
return nil, errors.New("management owner not found")
}
gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}
if gv.Group == capiControlPlanev1.GroupVersion.Group {
key := client.ObjectKey{
Namespace: capiMachine.Namespace,
Name: ref.Name,
}

controlPlane := &capiControlPlanev1.KubeadmControlPlane{}
if err := c.Get(ctx, key, controlPlane); err != nil {
return nil, err
}

return controlPlane, nil
}
return nil, nil
}

// IsOwnerDeleted returns a boolean if the owner of the CAPI machine has been deleted.
func IsOwnerDeleted(ctx context.Context, client client.Client, capiMachine *clusterv1.Machine) (bool, error) {
if util.IsControlPlaneMachine(capiMachine) {
// The controlplane sticks around after deletion pending the deletion of its machines.
// As such, need to check the deletion timestamp thereof.
if cp, err := getKubeadmControlPlaneFromCAPIMachine(ctx, client, capiMachine); cp != nil && cp.DeletionTimestamp == nil {
return false, nil
} else if err != nil && !strings.Contains(strings.ToLower(err.Error()), "not found") {
return false, err
}
} else {
// The machineset is deleted immediately, regardless of machine ownership.
// It is sufficient to check for its existence.
if ms, err := getMachineSetFromCAPIMachine(ctx, client, capiMachine); ms != nil {
return false, nil
} else if err != nil && !strings.Contains(strings.ToLower(err.Error()), "not found") {
return false, err
}
}
return true, nil
}

// fetchOwnerRef simply searches a list of OwnerReference objects for a given kind.
func fetchOwnerRef(refList []metav1.OwnerReference, kind string) *metav1.OwnerReference {
Expand Down Expand Up @@ -177,15 +94,6 @@ func ContainsAlreadyExistsSubstring(err error) bool {
return strings.Contains(strings.ToLower(err.Error()), "already exists")
}

// WithClusterSuffix appends a hyphen and the cluster name to a name if not already present.
func WithClusterSuffix(name string, clusterName string) string {
newName := name
if !strings.HasSuffix(name, "-"+clusterName) { // Add cluster name suffix if missing.
newName = name + "-" + clusterName
}
return newName
}

// GetOwnerClusterName returns the Cluster name of the cluster owning the current resource.
func GetOwnerClusterName(obj metav1.ObjectMeta) (string, error) {
for _, ref := range obj.GetOwnerReferences() {
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ require (
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/coredns/caddy v1.1.1 // indirect
github.com/coredns/corefile-migration v1.0.21 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
Expand All @@ -62,7 +60,6 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
Loading

0 comments on commit 655c562

Please sign in to comment.