From 22389770df9c4da53afb84fbdcb2c7f4b973a010 Mon Sep 17 00:00:00 2001 From: Marton Pentek Date: Mon, 26 Sep 2022 13:43:27 +0200 Subject: [PATCH] cleaning logs and error checking --- client/client.go | 19 ++++++++++++------- main.go | 27 +++++++++++++++------------ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/client/client.go b/client/client.go index 40eea33..7043ae5 100644 --- a/client/client.go +++ b/client/client.go @@ -81,12 +81,14 @@ func (c *SimplyClient) AddTxtRecord(FQDNName string, Value string, credentials C if err != nil { fmt.Println("Error on read: ", err) + return 0, err } var data CreateRecordResponse err = json.Unmarshal(responseData, &data) if err != nil { fmt.Println("Error on unmarshalling: ", err) + return 0, err } return data.Record.Id, nil } @@ -99,7 +101,7 @@ func (c *SimplyClient) RemoveTxtRecord(RecordId int, DnsName string, credentials response, err := client.Do(req) if err != nil || response.StatusCode != 200 { - fmt.Println("Error on request: ", err, " response: ", response.StatusCode) + fmt.Errorf("error on request(DELETE record): %v response: %d", err, response.StatusCode) return false } else { return true @@ -107,18 +109,20 @@ func (c *SimplyClient) RemoveTxtRecord(RecordId int, DnsName string, credentials } // GetTxtRecord Fetch TXT record by data returns id -func (c *SimplyClient) GetTxtRecord(TxtData string, FQDNName string, credentials Credentials) int { +func (c *SimplyClient) GetTxtRecord(TxtData string, FQDNName string, credentials Credentials) (int, error) { fqdnName := cutTrailingDotIfExist(FQDNName) req, err := http.NewRequest("GET", apiUrl+"/my/products/"+domainutil.Domain(fqdnName)+"/dns/records", nil) req.SetBasicAuth(credentials.AccountName, credentials.ApiKey) client := &http.Client{} response, err := client.Do(req) if err != nil || response.StatusCode != 200 { - fmt.Println("Error on request: ", err, " response: ", response.StatusCode) + fmt.Errorf("error on request(GET record): %v response: %d", err, response.StatusCode) + return 0, err } responseData, err := ioutil.ReadAll(response.Body) if err != nil { - fmt.Println("Error on read: ", err) + fmt.Errorf("error on read: %v", err) + return 0, err } var records RecordResponse @@ -132,10 +136,11 @@ func (c *SimplyClient) GetTxtRecord(TxtData string, FQDNName string, credentials } } } else { - panic(err) + fmt.Errorf("error on fecthing records: %v", err) + return 0, err } - return recordId + return recordId, nil } func (c *SimplyClient) UpdateTXTRecord(RecordId int, FQDNName string, Value string, credentials Credentials) (bool, error) { @@ -155,7 +160,7 @@ func (c *SimplyClient) UpdateTXTRecord(RecordId int, FQDNName string, Value stri response, err := client.Do(req) if err != nil || response.StatusCode != 200 { - fmt.Println("Error on request: ", err, " response: ", response.StatusCode) + fmt.Errorf("error on request(PUT Record): %v response: %d", err, response.StatusCode) return false, err } return true, nil diff --git a/main.go b/main.go index 1647ef5..77daa2b 100644 --- a/main.go +++ b/main.go @@ -51,14 +51,15 @@ func (e *SimplyDnsSolver) Present(ch *v1alpha1.ChallengeRequest) error { fmt.Println("Challenge being presented for: ", ch.ResolvedFQDN) cred, err := loadCredFromSecret(ch, e) if err != nil { - fmt.Println("Load credentials failed: ", err) + fmt.Errorf("load credentials failed(check secret configuration): %v", err) return err } id, err := e.client.AddTxtRecord(ch.ResolvedFQDN, ch.Key, cred) if err != nil { + fmt.Errorf("presenting challenge failed: %v", err) return err } else { - fmt.Println("Challenge have been created with id: ", id) + fmt.Println("Challenge have been created with record id: ", id) } return nil } @@ -67,14 +68,19 @@ func (e *SimplyDnsSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error { fmt.Println("Challenge being cleaned up...") cred, err := loadCredFromSecret(ch, e) if err != nil { - fmt.Println("Load credentials failed: ", err) + fmt.Errorf("load credentials failed: %v", err) return err } - Id := e.client.GetTxtRecord(ch.Key, ch.DNSName, cred) - fmt.Println("Record id ", Id, " fetched for cleanup.") + Id, err := e.client.GetTxtRecord(ch.Key, ch.DNSName, cred) + if err != nil { + fmt.Errorf("error on fetching record: %v", err) + return err + } + fmt.Println("Record(", Id, ") fetched for cleanup.") res := e.client.RemoveTxtRecord(Id, ch.DNSName, cred) if res == true { fmt.Println("Record(", Id, ") have been cleaned up.") + return err } return nil } @@ -83,7 +89,7 @@ func (e *SimplyDnsSolver) Initialize(kubeClientConfig *rest.Config, stopCh <-cha fmt.Println("Initializing...") cl, err := kubernetes.NewForConfig(kubeClientConfig) if err != nil { - fmt.Println("Init failed with error: ", err) + fmt.Errorf("init failed with error: %v", err) return err } e.kubeClient = cl @@ -114,8 +120,7 @@ func stringFromSecretData(secretData *map[string][]byte, key string) (string, er func loadCredFromSecret(ch *v1alpha1.ChallengeRequest, e *SimplyDnsSolver) (client.Credentials, error) { cfg, err := loadConfig(ch.Config) if err != nil { - fmt.Println("Error on reading config:") - fmt.Println(err) + fmt.Errorf("error on reading config: %v", err) return client.Credentials{}, err } secretName := cfg.SecretRef @@ -123,16 +128,14 @@ func loadCredFromSecret(ch *v1alpha1.ChallengeRequest, e *SimplyDnsSolver) (clie fmt.Println(secretName) sec, err := e.kubeClient.CoreV1().Secrets(ch.ResourceNamespace).Get(context.TODO(), secretName, metav1.GetOptions{}) if err != nil { - fmt.Println("Error on loading secret from kubernetes api:") - fmt.Println(err) + fmt.Errorf("error on loading secret from kubernetes api: %v", err) return client.Credentials{}, err } accountName, err := stringFromSecretData(&sec.Data, "account-name") apiKey, err := stringFromSecretData(&sec.Data, "api-key") if err != nil { - fmt.Println("Error on reading secret:") - fmt.Println(err) + fmt.Errorf("error on reading secret: %v", err) return client.Credentials{}, err }