Skip to content
This repository was archived by the owner on Sep 24, 2021. It is now read-only.

Commit a579084

Browse files
author
Amy Chen
committed
Add logger to actuators
1 parent b433f80 commit a579084

File tree

4 files changed

+54
-57
lines changed

4 files changed

+54
-57
lines changed

actuators/cluster.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,34 @@ limitations under the License.
1717
package actuators
1818

1919
import (
20-
"fmt"
21-
20+
"github.com/go-logr/logr"
2221
"sigs.k8s.io/cluster-api-provider-docker/kind/actions"
2322
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
2423
)
2524

2625
// Cluster defines a cluster actuator object
2726
type Cluster struct {
28-
}
29-
30-
// NewClusterActuator returns a new cluster actuator object
31-
func NewClusterActuator() *Cluster {
32-
return &Cluster{}
27+
Log logr.Logger
3328
}
3429

3530
// Reconcile setups an external load balancer for the cluster if needed
3631
func (c *Cluster) Reconcile(cluster *clusterv1.Cluster) error {
3732
elb, err := getExternalLoadBalancerNode(cluster.Name)
3833
if err != nil {
39-
fmt.Printf("%+v\n", err)
34+
c.Log.Error(err, "Error getting external load balancer node")
4035
return err
4136
}
4237
if elb != nil {
43-
fmt.Println("External Load Balancer already exists. Nothing to do for this cluster.")
38+
c.Log.Info("External Load Balancer already exists. Nothing to do for this cluster.")
4439
return nil
4540
}
46-
fmt.Printf("The cluster named %q has been created! Setting up some infrastructure.\n", cluster.Name)
41+
c.Log.Info("Cluster has been created! Setting up some infrastructure", "cluster-name", cluster.Name)
4742
_, err = actions.SetUpLoadBalancer(cluster.Name)
4843
return err
4944
}
5045

5146
// Delete can be used to delete a cluster
5247
func (c *Cluster) Delete(cluster *clusterv1.Cluster) error {
53-
fmt.Println("Cluster delete is not implemented.")
48+
c.Log.Info("Cluster delete is not implemented.")
5449
return nil
5550
}

actuators/machine.go

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"time"
2424

25+
"github.com/go-logr/logr"
2526
apicorev1 "k8s.io/api/core/v1"
2627
"k8s.io/apimachinery/pkg/types"
2728
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
@@ -45,116 +46,109 @@ const (
4546
type Machine struct {
4647
Core corev1.CoreV1Interface
4748
ClusterAPI v1alpha1.ClusterV1alpha1Interface
48-
}
49-
50-
// NewMachineActuator returns a new machine actuator object
51-
func NewMachineActuator(clusterapi v1alpha1.ClusterV1alpha1Interface, core corev1.CoreV1Interface) *Machine {
52-
return &Machine{
53-
Core: core,
54-
ClusterAPI: clusterapi,
55-
}
49+
Log logr.Logger
5650
}
5751

5852
// Create creates a machine for a given cluster
5953
// Note: have to print all the errors because cluster-api swallows them
6054
func (m *Machine) Create(ctx context.Context, c *clusterv1.Cluster, machine *clusterv1.Machine) error {
6155
old := machine.DeepCopy()
62-
fmt.Printf("Creating a machine for cluster %q\n", c.Name)
56+
m.Log.Info("Creating a machine for cluster", "cluster-name", c.Name)
6357
clusterExists, err := cluster.IsKnown(c.Name)
6458
if err != nil {
65-
fmt.Printf("%+v", err)
59+
m.Log.Error(err, "Error finding cluster-name", "cluster", c.Name)
6660
return err
6761
}
6862
// If there's no cluster, requeue the request until there is one
6963
if !clusterExists {
70-
fmt.Println("There is no cluster yet, waiting for a cluster before creating machines")
64+
m.Log.Info("There is no cluster yet, waiting for a cluster before creating machines")
7165
return &capierror.RequeueAfterError{RequeueAfter: 30 * time.Second}
7266
}
7367

7468
controlPlanes, err := actions.ListControlPlanes(c.Name)
7569
if err != nil {
76-
fmt.Printf("%+v\n", err)
70+
m.Log.Error(err, "Error listing control planes")
7771
return err
7872
}
79-
fmt.Printf("Is there a cluster? %v\n", clusterExists)
73+
m.Log.Info("Is there a cluster?", "cluster-exists", clusterExists)
8074
setValue := getRole(machine)
81-
fmt.Printf("This node has a role of %q\n", setValue)
75+
m.Log.Info("This node has a role", "role", setValue)
8276
if setValue == clusterAPIControlPlaneSetLabel {
8377
if len(controlPlanes) > 0 {
84-
fmt.Println("Adding a control plane")
78+
m.Log.Info("Adding a control plane")
8579
controlPlaneNode, err := actions.AddControlPlane(c.Name, machine.GetName(), machine.Spec.Versions.ControlPlane)
8680
if err != nil {
87-
fmt.Printf("%+v", err)
81+
m.Log.Error(err, "Error adding control plane")
8882
return err
8983
}
9084
nodeUID, err := actions.GetNodeRefUID(c.GetName(), controlPlaneNode.Name())
9185
if err != nil {
92-
fmt.Printf("%+v", err)
86+
m.Log.Error(err, "Error getting node reference UID")
9387
return err
9488
}
9589
providerID := providerID(controlPlaneNode.Name())
9690
machine.Spec.ProviderID = &providerID
9791
return m.save(old, machine, getNodeRef(controlPlaneNode.Name(), nodeUID))
9892
}
9993

100-
fmt.Println("Creating a brand new cluster")
94+
m.Log.Info("Creating a brand new cluster")
10195
elb, err := getExternalLoadBalancerNode(c.Name)
10296
if err != nil {
103-
fmt.Printf("%+v\n", err)
97+
m.Log.Error(err, "Error getting external load balancer node")
10498
return err
10599
}
106100
lbip, err := elb.IP()
107101
if err != nil {
108-
fmt.Printf("%+v\n", err)
102+
m.Log.Error(err, "Error getting node IP address")
109103
return err
110104
}
111105
controlPlaneNode, err := actions.CreateControlPlane(c.Name, machine.GetName(), lbip, machine.Spec.Versions.ControlPlane)
112106
if err != nil {
113-
fmt.Printf("%+v\n", err)
107+
m.Log.Error(err, "Error creating control plane")
114108
return err
115109
}
116110
nodeUID, err := actions.GetNodeRefUID(c.GetName(), controlPlaneNode.Name())
117111
if err != nil {
118-
fmt.Printf("%+v", err)
112+
m.Log.Error(err, "Error getting node reference UID")
119113
return err
120114
}
121115
// set the machine's providerID
122116
providerID := providerID(controlPlaneNode.Name())
123117
machine.Spec.ProviderID = &providerID
124118
if err := m.save(old, machine, getNodeRef(controlPlaneNode.Name(), nodeUID)); err != nil {
125-
fmt.Printf("%+v\n", err)
119+
m.Log.Error(err, "Error setting machine's provider ID")
126120
return err
127121
}
128122
s, err := kubeconfigToSecret(c.Name, c.Namespace)
129123
if err != nil {
130-
fmt.Printf("%+v\n", err)
124+
m.Log.Error(err, "Error converting kubeconfig to a secret")
131125
return err
132126
}
133127
// Save the secret to the management cluster
134128
if _, err := m.Core.Secrets(machine.GetNamespace()).Create(s); err != nil {
135-
fmt.Printf("%+v\n", err)
129+
m.Log.Error(err, "Error saving secret to management cluster")
136130
return err
137131
}
138132
return nil
139133
}
140134

141135
// If there are no control plane then we should hold off on joining workers
142136
if len(controlPlanes) == 0 {
143-
fmt.Printf("Sending machine %q back since there is no cluster to join\n", machine.Name)
137+
m.Log.Info("Sending machine back since there is no cluster to join", "machine", machine.Name)
144138
return &capierror.RequeueAfterError{RequeueAfter: 30 * time.Second}
145139
}
146140

147-
fmt.Println("Creating a new worker node")
141+
m.Log.Info("Creating a new worker node")
148142
worker, err := actions.AddWorker(c.Name, machine.GetName(), machine.Spec.Versions.Kubelet)
149143
if err != nil {
150-
fmt.Printf("%+v", err)
144+
m.Log.Error(err, "Error creating new worker node")
151145
return err
152146
}
153147
providerID := providerID(worker.Name())
154148
machine.Spec.ProviderID = &providerID
155149
nodeUID, err := actions.GetNodeRefUID(c.GetName(), worker.Name())
156150
if err != nil {
157-
fmt.Printf("%+v", err)
151+
m.Log.Error(err, "Error getting node reference ID")
158152
return err
159153
}
160154
return m.save(old, machine, getNodeRef(worker.Name(), nodeUID))
@@ -169,18 +163,18 @@ func (m *Machine) Delete(ctx context.Context, cluster *clusterv1.Cluster, machin
169163
if exists {
170164
setValue := getRole(machine)
171165
if setValue == clusterAPIControlPlaneSetLabel {
172-
fmt.Printf("Deleting a control plane: %q\n", machine.GetName())
166+
m.Log.Info("Deleting a control plane", "machine", machine.GetName())
173167
return actions.DeleteControlPlane(cluster.Name, machine.GetName())
174168
}
175-
fmt.Printf("Deleting a worker: %q\n", machine.GetName())
169+
m.Log.Info("Deleting a worker", "machine", machine.GetName())
176170
return actions.DeleteWorker(cluster.Name, machine.GetName())
177171
}
178172
return nil
179173
}
180174

181175
// Update updates a machine
182176
func (m *Machine) Update(ctx context.Context, cluster *clusterv1.Cluster, machine *clusterv1.Machine) error {
183-
fmt.Println("Update machine is not implemented yet.")
177+
m.Log.Info("Update machine is not implemented yet")
184178
return nil
185179
}
186180

@@ -197,41 +191,41 @@ func (m *Machine) Exists(ctx context.Context, cluster *clusterv1.Cluster, machin
197191
fmt.Sprintf("label=%s=%s", constants.ClusterLabelKey, cluster.Name),
198192
fmt.Sprintf("name=^%s$", machine.GetName()),
199193
}
200-
fmt.Printf("using labels: %v\n", labels)
194+
m.Log.Info("using labels", "labels", labels)
201195
nodeList, err := nodes.List(labels...)
202196
if err != nil {
203197
return false, err
204198
}
205-
fmt.Printf("found nodes: %v\n", nodeList)
199+
m.Log.Info("found nodes", "nodes", nodeList)
206200
return len(nodeList) >= 1, nil
207201
}
208202

209203
// patches the object and saves the status.
210204
func (m *Machine) save(old, new *clusterv1.Machine, noderef *apicorev1.ObjectReference) error {
211-
fmt.Println("updating machine")
205+
m.Log.Info("updating machine")
212206
p, err := patch.NewJSONPatch(old, new)
213207
if err != nil {
214-
fmt.Printf("%+v\n", err)
208+
m.Log.Error(err, "Error updating machine")
215209
return err
216210
}
217-
fmt.Println("Patches for machine", p)
211+
m.Log.Info("Patches for machine", "patches", p)
218212
if len(p) != 0 {
219213
pb, err := json.MarshalIndent(p, "", " ")
220214
if err != nil {
221-
fmt.Printf("%+v\n", err)
215+
m.Log.Error(err, "Error marshalling machine")
222216
return err
223217
}
224218
new, err = m.ClusterAPI.Machines(old.Namespace).Patch(new.Name, types.JSONPatchType, pb)
225219
if err != nil {
226-
fmt.Printf("%+v\n", err)
220+
m.Log.Error(err, "Error patching machine")
227221
return err
228222
}
229-
fmt.Println("updated machine")
223+
m.Log.Info("updated machine")
230224
}
231225
// set the noderef after so we don't try and patch it in during the first update
232226
new.Status.NodeRef = noderef
233227
if _, err := m.ClusterAPI.Machines(old.Namespace).UpdateStatus(new); err != nil {
234-
fmt.Printf("%+v\n", err)
228+
m.Log.Error(err, "Error setting node reference")
235229
return err
236230
}
237231
return nil

cmd/capd-manager/main.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"k8s.io/client-go/kubernetes"
24+
"k8s.io/klog/klogr"
2425
"sigs.k8s.io/cluster-api-provider-docker/actuators"
2526
"sigs.k8s.io/cluster-api/pkg/apis"
2627
"sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
@@ -57,19 +58,25 @@ func main() {
5758
panic(err)
5859
}
5960

60-
clusterActuator := actuators.NewClusterActuator()
61-
machineActuator := actuators.NewMachineActuator(cs.ClusterV1alpha1(), k8sclientset.CoreV1())
61+
clusterActuator := actuators.Cluster{
62+
Log: klogr.New().WithName("[cluster-actuator]"),
63+
}
64+
machineActuator := actuators.Machine{
65+
Core: k8sclientset.CoreV1(),
66+
ClusterAPI: cs.ClusterV1alpha1(),
67+
Log: klogr.New().WithName("[machine-actuator]"),
68+
}
6269

6370
// Register our cluster deployer (the interface is in clusterctl and we define the Deployer interface on the actuator)
6471
common.RegisterClusterProvisioner("aws", clusterActuator)
6572
if err := apis.AddToScheme(mgr.GetScheme()); err != nil {
6673
panic(err)
6774
}
6875

69-
if err := capimachine.AddWithActuator(mgr, machineActuator); err != nil {
76+
if err := capimachine.AddWithActuator(mgr, &machineActuator); err != nil {
7077
panic(err)
7178
}
72-
if err := capicluster.AddWithActuator(mgr, clusterActuator); err != nil {
79+
if err := capicluster.AddWithActuator(mgr, &clusterActuator); err != nil {
7380
panic(err)
7481
}
7582
fmt.Println("starting the controller...!")

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.12
44

55
require (
66
github.com/appscode/jsonpatch v0.0.0-20190108182946-7c0e3b262f30 // indirect
7-
github.com/go-logr/logr v0.1.0 // indirect
7+
github.com/go-logr/logr v0.1.0
88
github.com/go-logr/zapr v0.1.1 // indirect
99
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef // indirect
1010
github.com/google/btree v1.0.0 // indirect
@@ -25,6 +25,7 @@ require (
2525
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
2626
k8s.io/client-go v11.0.0+incompatible
2727
k8s.io/cluster-bootstrap v0.0.0-20181213155137-5f9271efc2e7 // indirect
28+
k8s.io/klog v0.3.0
2829
k8s.io/kubernetes v1.13.1
2930
sigs.k8s.io/cluster-api v0.0.0-20190607141803-aacb0c613ffb
3031
sigs.k8s.io/controller-runtime v0.1.10

0 commit comments

Comments
 (0)