Skip to content

Commit

Permalink
cleaning logs and error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
RunnerM committed Sep 26, 2022
1 parent f205ddf commit 2238977
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
19 changes: 12 additions & 7 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -99,26 +101,28 @@ 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
}
}

// 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
Expand All @@ -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) {
Expand All @@ -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
Expand Down
27 changes: 15 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -114,25 +120,22 @@ 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
fmt.Println("Secret reference:")
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
}

Expand Down

0 comments on commit 2238977

Please sign in to comment.