Skip to content

Commit f14a9ea

Browse files
committed
updated error handling
1 parent eb56897 commit f14a9ea

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed

main.go

+16-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
// This program generates Kubeconfig files based on the CSR API.
12
// Based on Code from https://medium.com/@elfakharany/automate-kubernetes-user-creation-using-the-native-go-client-e2d20dcdc9de
2-
// Updated to take account of change API versions
3+
// Updated to take account of change API versions.
34

45
package main
56

@@ -18,7 +19,7 @@ import (
1819
"path/filepath"
1920
"time"
2021

21-
"github.com/rs/zerolog/log"
22+
"log"
2223

2324
"gopkg.in/yaml.v2"
2425
certificates "k8s.io/api/certificates/v1"
@@ -81,12 +82,12 @@ func initKubeClient() (*kubernetes.Clientset, clientcmd.ClientConfig, error) {
8182
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{})
8283
config, err := kubeConfig.ClientConfig()
8384
if err != nil {
84-
log.Printf("initKubeClient: failed creating ClientConfig with", err)
85+
log.Printf("initKubeClient: failed creating ClientConfig with %s", err)
8586
return nil, nil, err
8687
}
8788
clientset, err := kubernetes.NewForConfig(config)
8889
if err != nil {
89-
log.Printf("initKubeClient: failed creating Clientset with", err)
90+
log.Printf("initKubeClient: failed creating Clientset with %s", err)
9091
return nil, nil, err
9192
}
9293
return clientset, kubeConfig, nil
@@ -111,8 +112,7 @@ func main() {
111112

112113
key, err := rsa.GenerateKey(rand.Reader, 1024)
113114
if err != nil {
114-
log.Print("Error Generating key")
115-
log.Printf("Error %s", err)
115+
log.Fatalf("Error Generating key : %s", err)
116116
}
117117
keyDer := x509.MarshalPKCS1PrivateKey(key)
118118

@@ -131,14 +131,12 @@ func main() {
131131
}
132132
bytes, err := x509.CreateCertificateRequest(rand.Reader, &csrReq, key)
133133
if err != nil {
134-
log.Print("Error Creating Certificate Request")
135-
log.Printf("Error %s", err)
134+
log.Fatalf("Error Creating Certificate Request %s", err)
136135
}
137136

138137
clientset, config, err := initKubeClient()
139138
if err != nil {
140-
fmt.Println("Error initializing Kubernetes client")
141-
log.Printf("Error %s", err)
139+
log.Fatalf("Error initializing Kubeclient %s", err)
142140
}
143141
csr := &certificates.CertificateSigningRequest{
144142
ObjectMeta: v1.ObjectMeta{
@@ -162,7 +160,7 @@ func main() {
162160
_, err = clientset.CertificatesV1().CertificateSigningRequests().Create(context.TODO(), csr, v1.CreateOptions{})
163161
if err != nil {
164162
log.Print("Error Creating CSR Object. Are you running on a cluste < 1.19? This only works with 1.19+")
165-
log.Printf("Error %s", err)
163+
log.Fatalf("Error %s", err)
166164
}
167165
csr.Status.Conditions = append(csr.Status.Conditions, certificates.CertificateSigningRequestCondition{
168166
Type: certificates.CertificateApproved,
@@ -173,8 +171,7 @@ func main() {
173171
})
174172
csr, err = clientset.CertificatesV1().CertificateSigningRequests().UpdateApproval(context.Background(), "tempcsr", csr, v1.UpdateOptions{})
175173
if err != nil {
176-
fmt.Println("Error Approving Certificate")
177-
log.Printf("Error %s", err)
174+
log.Fatalf("Error Approving Certificate : %s", err)
178175
}
179176
// Give the API server a couple of seconds to issue the cert.
180177
time.Sleep(2 * time.Second)
@@ -187,8 +184,7 @@ func main() {
187184
}
188185
issued_cert, err := x509.ParseCertificate(pb.Bytes)
189186
if err != nil {
190-
log.Print("Error Parsing Certificate")
191-
log.Print(err)
187+
log.Fatalf("Error Parsing Certificate %s", err)
192188
}
193189
issued_group := "none"
194190
if issued_cert.Subject.Organization[0] != "" {
@@ -198,8 +194,7 @@ func main() {
198194

199195
raw, err := config.RawConfig()
200196
if err != nil {
201-
log.Print("error getting raw config")
202-
log.Print(err)
197+
log.Fatalf("error getting raw config %s", err)
203198
}
204199
cluster := raw.Contexts[raw.CurrentContext].Cluster
205200

@@ -238,25 +233,21 @@ func main() {
238233

239234
dir, err := os.Getwd()
240235
if err != nil {
241-
log.Print("Error Getting working directory")
242-
log.Print(err)
236+
log.Fatalf("Error Getting working directory %s", err)
243237
}
244238
_, err = os.Create(filepath.Join(dir, *outputFile))
245239
if err != nil {
246-
log.Print("Error Creating output file")
247-
log.Print(err)
240+
log.Fatalf("Error Creating output file %s", err)
248241
}
249242
file, err := os.OpenFile(*outputFile, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
250243
if err != nil {
251-
log.Print("Error opening output file")
252-
log.Print(err)
244+
log.Fatalf("Error opening output file %s", err)
253245
}
254246
defer file.Close()
255247
e := yaml.NewEncoder(file)
256248
err = e.Encode(kc)
257249
if err != nil {
258-
log.Print("Error encoding Kubeconfig YAML")
259-
log.Print(err)
250+
log.Fatalf("Error encoding Kubeconfig YAML %s", err)
260251
}
261252
clientset.CertificatesV1().CertificateSigningRequests().Delete(context.TODO(), csr.GetName(), v1.DeleteOptions{})
262253

0 commit comments

Comments
 (0)