Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Commit 859485a

Browse files
committed
Replace all logging with klog
1 parent 15fe509 commit 859485a

File tree

4 files changed

+65
-27
lines changed

4 files changed

+65
-27
lines changed

pkg/cloud/packet/actuators/cluster/actuator.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ limitations under the License.
1717
package cluster
1818

1919
import (
20+
"errors"
2021
"fmt"
21-
"log"
2222
"reflect"
2323
"time"
2424

2525
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/ca"
2626
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/deployer"
2727
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/util"
28+
"k8s.io/klog"
2829
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
2930
client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
3031
controllerError "sigs.k8s.io/cluster-api/pkg/controller/error"
@@ -58,7 +59,7 @@ func NewActuator(params ActuatorParams) (*Actuator, error) {
5859

5960
// Reconcile reconciles a cluster and is invoked by the Cluster Controller
6061
func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
61-
log.Printf("Reconciling cluster %v.", cluster.Name)
62+
klog.Infof("Reconciling cluster %v.", cluster.Name)
6263
// save the original status
6364
clusterCopy := cluster.DeepCopy()
6465
// get a client we can use
@@ -87,11 +88,11 @@ func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
8788
return fmt.Errorf("unable to convert newly generated provider spec with CA key/certificate to provider config for %s: %v", cluster.Name, err)
8889
}
8990
cluster.Spec.ProviderSpec = spec
90-
log.Printf("saving updated cluster spec %s", cluster.Name)
91+
klog.Infof("saving updated cluster spec %s", cluster.Name)
9192
if updatedCluster, err = clusterClient.Update(cluster); err != nil {
9293
msg := fmt.Sprintf("failed to save updated cluster %s: %v", cluster.Name, err)
93-
log.Printf(msg)
94-
return fmt.Errorf(msg)
94+
klog.Info(msg)
95+
return errors.New(msg)
9596
}
9697
cluster = updatedCluster
9798
}
@@ -116,19 +117,22 @@ func (a *Actuator) Reconcile(cluster *clusterv1.Cluster) error {
116117
}
117118

118119
if !reflect.DeepEqual(cluster.Status, clusterCopy.Status) {
119-
log.Printf("saving updated cluster status %s", cluster.Name)
120+
klog.Infof("saving updated cluster status %s", cluster.Name)
120121
if _, err := clusterClient.UpdateStatus(cluster); err != nil {
121122
msg := fmt.Sprintf("failed to save updated cluster status %s: %v", cluster.Name, err)
122-
log.Printf(msg)
123-
return fmt.Errorf(msg)
123+
klog.Info(msg)
124+
return errors.New(msg)
124125
}
126+
klog.Infof("successfully updated cluster status %s", cluster.Name)
125127
}
126128

129+
klog.Infof("cluster reconcile complete: %s", cluster.Name)
130+
127131
return nil
128132
}
129133

130134
// Delete deletes a cluster and is invoked by the Cluster Controller
131135
func (a *Actuator) Delete(cluster *clusterv1.Cluster) error {
132-
log.Printf("Deleting cluster %v.", cluster.Name)
136+
klog.Infof("Deleting cluster %v.", cluster.Name)
133137
return nil
134138
}

pkg/cloud/packet/actuators/machine/actuator.go

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ package machine
1818

1919
import (
2020
"context"
21+
"errors"
2122
"fmt"
22-
"log"
2323
"strings"
2424
"time"
2525

@@ -29,6 +29,7 @@ import (
2929
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/deployer"
3030
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/util"
3131
"github.com/packethost/packngo"
32+
"k8s.io/klog"
3233
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
3334
client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1"
3435
capiutil "sigs.k8s.io/cluster-api/pkg/util"
@@ -82,13 +83,19 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
8283
if machine == nil {
8384
return fmt.Errorf("cannot create nil machine")
8485
}
86+
var msg string
87+
klog.Infof("Create machine %s", machine.Name)
8588
machineConfig, err := util.MachineProviderFromProviderConfig(machine.Spec.ProviderSpec)
8689
if err != nil {
87-
return fmt.Errorf("Unable to read providerSpec from machine config: %v", err)
90+
msg = fmt.Sprintf("Unable to read providerSpec from machine config: %v", err)
91+
klog.Info(msg)
92+
return errors.New(msg)
8893
}
8994
clusterConfig, err := util.ClusterProviderFromProviderConfig(cluster.Spec.ProviderSpec)
9095
if err != nil {
91-
return fmt.Errorf("unable to unpack cluster provider: %v", err)
96+
msg = fmt.Sprintf("Error reading cluster provider config: %v", err)
97+
klog.Info(msg)
98+
return errors.New(msg)
9299
}
93100

94101
// generate a unique UID that will survive pivot, i.e. is not tied to the cluster itself
@@ -102,7 +109,9 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
102109
// first we need to find the correct userdata
103110
userdataTmpl, containerRuntime, err := a.machineConfigGetter.GetUserdata(machineConfig.OS, machine.Spec.Versions)
104111
if err != nil {
105-
return fmt.Errorf("Unable to read userdata: %v", err)
112+
msg = fmt.Sprintf("Unable to read userdata: %v", err)
113+
klog.Info(msg)
114+
return errors.New(msg)
106115
}
107116
var (
108117
token = ""
@@ -111,27 +120,37 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
111120
caKey []byte
112121
)
113122
if machine.Spec.Versions.ControlPlane != "" {
123+
klog.Infof("building master controlplane node: %s", machine.Name)
114124
role = "master"
115125
caCert = clusterConfig.CAKeyPair.Cert
116126
caKey = clusterConfig.CAKeyPair.Key
117127
if len(caCert) == 0 {
118-
return fmt.Errorf("CA Certificate not yet created")
128+
msg = fmt.Sprintf("CA Certificate not yet created for cluster %s when building machine: %s", cluster.Name, machine.Name)
129+
klog.Info(msg)
130+
return errors.New(msg)
119131
}
120132
if len(caKey) == 0 {
121-
return fmt.Errorf("CA Key not yet created")
133+
msg = fmt.Sprintf("CA Key not yet created for cluster %s when building machine: %s", cluster.Name, machine.Name)
134+
klog.Info(msg)
135+
return errors.New(msg)
122136
}
123137
tags = append(tags, util.MasterTag)
124138
} else {
139+
klog.Infof("building worker controlplane node: %s", machine.Name)
125140
token, err = a.deployer.NewBootstrapToken(cluster)
126141
if err != nil {
127-
return fmt.Errorf("failed to create and save token for cluster %q: %v", cluster.Name, err)
142+
msg = fmt.Sprintf("failed to create and save token for cluster %q: %v", cluster.Name, err)
143+
klog.Info(msg)
144+
return errors.New(msg)
128145
}
129146
tags = append(tags, util.WorkerTag)
130147
}
131148

132149
userdata, err := parseUserdata(userdataTmpl, role, cluster, machine, machineConfig.OS, token, caCert, caKey, a.controlPort, containerRuntime)
133150
if err != nil {
134-
return fmt.Errorf("Unable to generate userdata: %v", err)
151+
msg = fmt.Sprintf("Unable to generate userdata for machine %s: %v", machine.Name, err)
152+
klog.Info(msg)
153+
return errors.New(msg)
135154
}
136155

137156
log.Printf("Creating machine %v for cluster %v.", machine.Name, cluster.Name)
@@ -148,11 +167,13 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
148167

149168
device, _, err := a.packetClient.Devices.Create(serverCreateOpts)
150169
if err != nil {
151-
return fmt.Errorf("failed to create server: %v", err)
170+
msg = fmt.Sprintf("failed to create machine %s: %v", machine.Name, err)
171+
klog.Info(msg)
172+
return errors.New(msg)
152173
}
153174

154175
// we need to loop here until the device exists and has an IP address
155-
log.Printf("Created device, waiting for it to be ready")
176+
log.Printf("Created device %s, waiting for it to be ready", machine.Name)
156177
a.waitForMachineReady(device)
157178

158179
// add the annotations so that cluster-api knows it is there (also, because it is useful to have)
@@ -167,6 +188,8 @@ func (a *Actuator) Create(ctx context.Context, cluster *clusterv1.Cluster, machi
167188
if _, err = a.updateMachine(cluster, machine); err != nil {
168189
return fmt.Errorf("error updating Machine object with annotations: %v", err)
169190
}
191+
msg = fmt.Sprintf("machine successfully created and annotated %s", machine.Name)
192+
klog.Info(msg)
170193

171194
return nil
172195
}
@@ -248,15 +271,22 @@ func (a *Actuator) Exists(ctx context.Context, cluster *clusterv1.Cluster, machi
248271
if machine == nil {
249272
return false, fmt.Errorf("cannot check if nil machine exists")
250273
}
274+
var msg string
251275
log.Printf("Checking if machine %v for cluster %v exists.", machine.Name, cluster.Name)
252276
device, err := a.packetClient.GetDevice(machine)
253277
if err != nil {
254-
return false, fmt.Errorf("error retrieving machine status %s: %v", machine.Name, err)
278+
msg = fmt.Sprintf("error retrieving machine status %s: %v", machine.Name, err)
279+
klog.Info(msg)
280+
return false, errors.New(msg)
255281
}
256282
if device == nil {
283+
msg = fmt.Sprintf("machine not found %s", machine.Name)
284+
klog.Info(msg)
257285
return false, nil
258286
}
259287

288+
msg = fmt.Sprintf("machine found %s: %s", machine.Name, device.ID)
289+
klog.Info(msg)
260290
return true, nil
261291
}
262292

@@ -289,7 +319,7 @@ func (a *Actuator) updateMachine(cluster *clusterv1.Cluster, machine *clusterv1.
289319

290320
func (a *Actuator) waitForMachineReady(device *packngo.Device) error {
291321
err := capiutil.PollImmediate(retryIntervalMachineReady, timeoutMachineReady, func() (bool, error) {
292-
fmt.Printf("Waiting for device %v to become ready...", device.ID)
322+
klog.Infof("Waiting for device %v to become ready...", device.ID)
293323
dev, _, err := a.packetClient.Devices.Get(device.ID, nil)
294324
if err != nil {
295325
return false, nil

pkg/cloud/packet/client.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet/util"
99
"github.com/packethost/packngo"
10+
"k8s.io/klog"
1011
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
1112
)
1213

@@ -43,30 +44,33 @@ func (p *PacketClient) GetDevice(machine *clusterv1.Machine) (*packngo.Device, e
4344
}
4445
// if there are no annotations, or the annotation we want does not exist, nothing to do
4546
if machine.Annotations == nil {
46-
fmt.Printf("No annotations with machine UID for %s, machine does not exist", machine.Name)
47+
klog.Infof("No annotations with machine UID for %s, machine does not exist", machine.Name)
4748
return nil, nil
4849
}
4950
var (
5051
mUID string
5152
ok bool
5253
)
5354
if mUID, ok = machine.Annotations[util.AnnotationUID]; !ok {
54-
fmt.Printf("No UID annotation %s with machine UID for %s, machine does not exist", util.AnnotationUID, machine.Name)
55+
klog.Infof("No UID annotation %s with machine UID for %s, machine does not exist", util.AnnotationUID, machine.Name)
5556
return nil, nil
5657
}
5758
tag := util.GenerateMachineTag(mUID)
5859
return p.GetDeviceByTags(c.ProjectID, []string{tag})
5960
}
6061
func (p *PacketClient) GetDeviceByTags(project string, tags []string) (*packngo.Device, error) {
62+
klog.Infof("getting device by tags for project %s, tags %v", project, tags)
6163
devices, _, err := p.Devices.List(project, nil)
6264
if err != nil {
6365
return nil, fmt.Errorf("Error retrieving devices: %v", err)
6466
}
6567
// returns the first one that matches all of the tags
6668
for _, device := range devices {
6769
if util.ItemsInList(device.Tags, tags) {
70+
klog.Infof("found device %s", device.ID)
6871
return &device, nil
6972
}
7073
}
74+
klog.Info("no devices found")
7175
return nil, nil
7276
}

pkg/cloud/packet/deployer/deploy.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"crypto/x509"
2121
"encoding/pem"
2222
"fmt"
23-
"log"
2423
"time"
2524

2625
"github.com/packethost/cluster-api-provider-packet/pkg/cloud/packet"
@@ -31,6 +30,7 @@ import (
3130
corev1 "k8s.io/client-go/kubernetes/typed/core/v1"
3231
"k8s.io/client-go/tools/clientcmd"
3332
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
33+
"k8s.io/klog"
3434
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
3535
)
3636

@@ -71,7 +71,7 @@ func (d *Deployer) GetIP(cluster *clusterv1.Cluster, machine *clusterv1.Machine)
7171
machineRef string
7272
)
7373
if machine != nil {
74-
log.Printf("Getting IP of machine %v for cluster %v.", machine.Name, cluster.Name)
74+
klog.Infof("Getting IP of machine %v for cluster %v.", machine.Name, cluster.Name)
7575
// if there are no annotations, or the annotation we want does not exist, nothing to do
7676
if machine.Annotations == nil {
7777
return "", fmt.Errorf("No annotations with machine UID for %s", machine.Name)
@@ -86,7 +86,7 @@ func (d *Deployer) GetIP(cluster *clusterv1.Cluster, machine *clusterv1.Machine)
8686
machineRef = mUID
8787
device, err = d.client.GetDevice(machine)
8888
} else {
89-
log.Printf("Getting IP of any master machine for cluster %v.", cluster.Name)
89+
klog.Infof("Getting IP of any master machine for cluster %v.", cluster.Name)
9090
machineRef = fmt.Sprintf("master for cluster %s", cluster.Name)
9191
tags := []string{
9292
util.GenerateClusterTag(string(cluster.Name)),
@@ -119,7 +119,7 @@ func (d *Deployer) GetKubeConfig(cluster *clusterv1.Cluster, master *clusterv1.M
119119
if cluster == nil {
120120
return "", fmt.Errorf("cannot get kubeconfig for nil cluster")
121121
}
122-
log.Printf("Getting KubeConfig for cluster %v.", cluster.Name)
122+
klog.Infof("Getting KubeConfig for cluster %v.", cluster.Name)
123123

124124
// use local var to allow for easier future changes
125125
userName := adminUserName

0 commit comments

Comments
 (0)