Skip to content
Open
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: 17 additions & 6 deletions controllers/maascluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ func (r *MaasClusterReconciler) reconcileDNSAttachments(clusterScope *scope.Clus
continue
}

//TODO: PCP-22 Add loadbalancer address here
lbIP := "10.11.130.190"
if !currentIPs.Has(lbIP) {
runningIpAddresses = append(runningIpAddresses, lbIP)
} else {
machinesPendingAttachment = append(machinesPendingAttachment, m)
}

machineIP := getExternalMachineIP(m)
attached := currentIPs.Has(machineIP)
isRunningHealthy := IsRunning(m)
Expand Down Expand Up @@ -238,18 +246,21 @@ func (r *MaasClusterReconciler) reconcileNormal(_ context.Context, clusterScope

dnsService := dns.NewService(clusterScope)

if err := dnsService.ReconcileDNS(); err != nil {
clusterScope.Error(err, "failed to reconcile load balancer")
conditions.MarkFalse(maasCluster, infrav1beta1.DNSReadyCondition, infrav1beta1.DNSFailedReason, clusterv1.ConditionSeverityError, err.Error())
return reconcile.Result{}, err
}
//TODO: Should it be removed
//if err := dnsService.ReconcileDNS(); err != nil {
// clusterScope.Error(err, "failed to reconcile load balancer")
// conditions.MarkFalse(maasCluster, infrav1beta1.DNSReadyCondition, infrav1beta1.DNSFailedReason, clusterv1.ConditionSeverityError, err.Error())
// return reconcile.Result{}, err
//}

if maasCluster.Status.Network.DNSName == "" {
maasCluster.Status.Network.DNSName = "10.11.130.190"
conditions.MarkFalse(maasCluster, infrav1beta1.DNSReadyCondition, infrav1beta1.WaitForDNSNameReason, clusterv1.ConditionSeverityInfo, "")
clusterScope.Info("Waiting on API server DNS name")
return reconcile.Result{RequeueAfter: 15 * time.Second}, nil
}

//TODO: Check what is getting set here it should be controlPlaneEndpoint: 10.11.130.190:6443
maasCluster.Spec.ControlPlaneEndpoint = infrav1beta1.APIEndpoint{
Host: maasCluster.Status.Network.DNSName,
Port: clusterScope.APIServerPort(),
Expand All @@ -260,6 +271,7 @@ func (r *MaasClusterReconciler) reconcileNormal(_ context.Context, clusterScope
// Mark the maasCluster ready
conditions.MarkTrue(maasCluster, infrav1beta1.DNSReadyCondition)

//TODO: PCP-22
if err := r.reconcileDNSAttachments(clusterScope, dnsService); err != nil {
if errors.Is(err, ErrRequeueDNS) {
return ctrl.Result{}, nil
Expand All @@ -268,7 +280,6 @@ func (r *MaasClusterReconciler) reconcileNormal(_ context.Context, clusterScope

clusterScope.Error(err, "failed to reconcile load balancer")
return reconcile.Result{}, err

}

clusterScope.ReconcileMaasClusterWhenAPIServerIsOnline()
Expand Down
16 changes: 13 additions & 3 deletions controllers/maasmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/base64"
"fmt"
maasdns "github.com/spectrocloud/cluster-api-provider-maas/pkg/maas/dns"
"k8s.io/apimachinery/pkg/runtime"
"time"

Expand All @@ -45,7 +46,6 @@ import (

//infrav1alpha3 "github.com/spectrocloud/cluster-api-provider-maas/api/v1alpha3"
infrav1beta1 "github.com/spectrocloud/cluster-api-provider-maas/api/v1beta1"
maasdns "github.com/spectrocloud/cluster-api-provider-maas/pkg/maas/dns"
maasmachine "github.com/spectrocloud/cluster-api-provider-maas/pkg/maas/machine"
"github.com/spectrocloud/cluster-api-provider-maas/pkg/maas/scope"
)
Expand Down Expand Up @@ -326,7 +326,10 @@ func (r *MaasMachineReconciler) reconcileNormal(_ context.Context, machineScope
// TODO(saamalik) tags / labels

// Set the address if good
machineScope.Info("TESTING..... Set machine address")
machineScope.SetAddresses(m.Addresses)
//TODO: PCP-22 Add loadbalancer address here
//machineScope.SetAddresses([]clusterv1.MachineAddress{{Type: clusterv1.MachineExternalIP, Address: "10.11.130.190"}})

if err := r.reconcileDNSAttachment(machineScope, clusterScope, m); err != nil {
if errors.Is(err, ErrRequeueDNS) {
Expand Down Expand Up @@ -401,7 +404,11 @@ func (r *MaasMachineReconciler) reconcileDNSAttachment(machineScope *scope.Machi
return errors.Wrapf(err, "machine %q - error determining registration status", m.ID)
}

machineScope.MaasMachine.Status.DNSAttached = registered
//TODO: PCP-22
machineScope.Info("TESTING.... Set machineScope.MaasMachine.Status.DNSAttached to true")
//registered := true

//machineScope.MaasMachine.Status.DNSAttached = registered

if registered {
// Wait for Cluster to delete this guy
Expand All @@ -421,7 +428,10 @@ func (r *MaasMachineReconciler) reconcileDNSAttachment(machineScope *scope.Machi
return errors.Wrapf(err, "normal machine %q - error determining registration status", m.ID)
}

machineScope.MaasMachine.Status.DNSAttached = registered
//TODO: PCP-22
machineScope.Info("TESTING.... Set machineScope.MaasMachine.Status.DNSAttached to true")
//registered := true
//machineScope.MaasMachine.Status.DNSAttached = registered

if !registered {
conditions.MarkFalse(machineScope.MaasMachine, infrav1beta1.DNSAttachedCondition, infrav1beta1.DNSAttachPending, clusterv1.ConditionSeverityWarning, "")
Expand Down
130 changes: 69 additions & 61 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,70 +102,78 @@ func main() {
//ctx := ctrl.SetupSignalHandler()
ctx := context.Background()

if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create ready check")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create health check")
os.Exit(1)
}

// Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers
// requiring a connection to a remote cluster
log := ctrl.Log.WithName("remote").WithName("ClusterCacheTracker")
tracker, err := remote.NewClusterCacheTracker(
mgr,
remote.ClusterCacheTrackerOptions{
Log: &log,
Indexes: remote.DefaultIndexes,
},
)
if err != nil {
setupLog.Error(err, "unable to create cluster cache tracker")
os.Exit(1)
}
if err := (&remote.ClusterCacheReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, concurrency(1)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterCacheReconciler")
os.Exit(1)
if webhookPort != 0 {
if err := mgr.AddReadyzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create ready check")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("ping", healthz.Ping); err != nil {
setupLog.Error(err, "unable to create health check")
os.Exit(1)
}
}

if err := (&controllers.MaasMachineReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MaasMachine"),
Recorder: mgr.GetEventRecorderFor("maasmachine-controller"),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, concurrency(machineConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MaasMachine")
os.Exit(1)
}

if err := (&controllers.MaasClusterReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MaasCluster"),
Recorder: mgr.GetEventRecorderFor("maascluster-controller"),
Tracker: tracker,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MaasCluster")
os.Exit(1)
if webhookPort == 0 {
// Set up a ClusterCacheTracker and ClusterCacheReconciler to provide to controllers

// requiring a connection to a remote cluster
log := ctrl.Log.WithName("remote").WithName("ClusterCacheTracker")
tracker, err := remote.NewClusterCacheTracker(
mgr,
remote.ClusterCacheTrackerOptions{
Log: &log,
Indexes: remote.DefaultIndexes,
},
)
if err != nil {
setupLog.Error(err, "unable to create cluster cache tracker")
os.Exit(1)
}
if err := (&remote.ClusterCacheReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("remote").WithName("ClusterCacheReconciler"),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, concurrency(1)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "ClusterCacheReconciler")
os.Exit(1)
}

if err := (&controllers.MaasMachineReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MaasMachine"),
Recorder: mgr.GetEventRecorderFor("maasmachine-controller"),
Tracker: tracker,
}).SetupWithManager(ctx, mgr, concurrency(machineConcurrency)); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MaasMachine")
os.Exit(1)
}

if err := (&controllers.MaasClusterReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("MaasCluster"),
Recorder: mgr.GetEventRecorderFor("maascluster-controller"),
Tracker: tracker,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "MaasCluster")
os.Exit(1)
}
}

if err = (&infrav1beta1.MaasCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "MaasCluster")
os.Exit(1)
}
if err = (&infrav1beta1.MaasMachine{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "MaasMachine")
os.Exit(1)
}
if err = (&infrav1beta1.MaasMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "MaasMachineTemplate")
os.Exit(1)
//TODO: Do we need to check webhook port here ??
if webhookPort != 0 {
if err = (&infrav1beta1.MaasCluster{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "MaasCluster")
os.Exit(1)
}
if err = (&infrav1beta1.MaasMachine{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "MaasMachine")
os.Exit(1)
}
if err = (&infrav1beta1.MaasMachineTemplate{}).SetupWebhookWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create webhook", "webhook", "MaasMachineTemplate")
os.Exit(1)
}
}
// +kubebuilder:scaffold:builder

Expand All @@ -189,7 +197,7 @@ func initFlags(fs *pflag.FlagSet) {
"The minimum interval at which watched resources are reconciled (e.g. 15m)")
fs.StringVar(&healthAddr, "health-addr", ":9440",
"The address the health endpoint binds to.")
fs.IntVar(&webhookPort, "webhook-port", 9443,
fs.IntVar(&webhookPort, "webhook-port", 0,
"Webhook Server port")
fs.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.",
Expand Down
32 changes: 30 additions & 2 deletions pkg/maas/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func (s *Service) ReconcileDNS() error {
s.scope.V(2).Info("Reconciling DNS")
ctx := context.TODO()

//TODO: PCP-22
dnsResource, err := s.GetDNSResource()
if err != nil && !errors.Is(err, ErrNotFound) {
return err
Expand All @@ -37,7 +38,7 @@ func (s *Service) ReconcileDNS() error {
dnsName := s.scope.GetDNSName()

if dnsResource == nil {
if _, err = s.maasClient.DNSResources().
if _, err := s.maasClient.DNSResources().
Builder().
WithFQDN(s.scope.GetDNSName()).
WithAddressTTL("10").
Expand All @@ -47,6 +48,7 @@ func (s *Service) ReconcileDNS() error {
}
}

//TODO: PCP-22
s.scope.SetDNSName(dnsName)

return nil
Expand All @@ -55,6 +57,7 @@ func (s *Service) ReconcileDNS() error {
// UpdateAttachments reconciles the load balancers for the given cluster.
func (s *Service) UpdateDNSAttachments(IPs []string) error {
s.scope.V(2).Info("Updating DNS Attachments")
//TODO: PCP-22
ctx := context.TODO()
// get ID of loadbalancer
dnsResource, err := s.GetDNSResource()
Expand Down Expand Up @@ -94,7 +97,9 @@ func (s *Service) MachineIsRegisteredWithAPIServerDNS(i *infrainfrav1beta1.Machi
}

for _, mAddress := range i.Addresses {
if ips.Has(mAddress.Address) {
s.scope.Info("TESTING....", "DNS IPs:", mAddress)
if ips.Has(mAddress.Address) || ips.Has("10.11.130.190") {
s.scope.Info("TESTING....", "DNS Record found for", mAddress.Address, "or 10.11.130.190")
return true, nil
}
}
Expand All @@ -109,11 +114,14 @@ func (s *Service) GetAPIServerDNSRecords() (sets.String, error) {
}

ips := sets.NewString()

//TODO: PCP-22
for _, address := range dnsResource.IPAddresses() {
if address.IP().String() != "" {
ips.Insert(address.IP().String())
}
}
ips.Insert("10.11.130.190")

return ips, nil
}
Expand All @@ -124,9 +132,28 @@ func (s *Service) GetDNSResource() (maasclient.DNSResource, error) {
return nil, errors.New("No DNS on the cluster set!")
}

s.scope.Info("TESTING.... ", "dnsName: ", dnsName)
d, err := s.maasClient.DNSResources().
List(context.Background(),
maasclient.ParamsBuilder().Set(maasclient.FQDNKey, dnsName))

//s.scope.MaasCluster.Spec.ControlPlaneEndpoint.Host = "10.11.130.190"

//if err != nil {
//
// d2, err := s.maasClient.DNSResources().
// Builder().
// WithFQDN(s.scope.GetDNSName()).
// WithAddressTTL("20").
// WithIPAddresses([]string{"10.11.130.190"}).
// Create(context.Background())
// if err != nil {
// return nil, errors.Wrapf(err, "TESTING.... Unable to create DNS Resources")
// }
// s.scope.Info("TESTING.... ", "Create DNS resource if not found", d2)
// return d2, nil
//}

if err != nil {
return nil, errors.Wrapf(err, "error retrieving dns resources %q", dnsName)
} else if len(d) > 1 {
Expand All @@ -135,5 +162,6 @@ func (s *Service) GetDNSResource() (maasclient.DNSResource, error) {
return nil, ErrNotFound
}

s.scope.Info("TESTING.... ", "d[0]: ", d[0])
return d[0], nil
}
Loading