penguin/webhook-freedns

freedns webhook for cert-manager

commit 2da56a0aafcb263df2027fc64b6d6959a53b3e21

author斟酌 鵬兄 <tgckpg@gmail.com>
date2024-11-01T19:47:42Z
subjectProperly implements logging
commit 2da56a0aafcb263df2027fc64b6d6959a53b3e21
Author: 斟酌 鵬兄 <tgckpg@gmail.com>
Date:   2024-11-01T19:47:42Z

    Properly implements logging
---
 deploy/freedns-webhook/templates/deployment.yaml |  1 +
 deploy/freedns-webhook/values.yaml               |  2 +-
 freedns/freedns.go                               | 26 ++++++++++--------------
 main.go                                          |  5 ++---
 4 files changed, 15 insertions(+), 19 deletions(-)

diff --git a/deploy/freedns-webhook/templates/deployment.yaml b/deploy/freedns-webhook/templates/deployment.yaml
index 5c49d7b..43d6f79 100644
--- a/deploy/freedns-webhook/templates/deployment.yaml
+++ b/deploy/freedns-webhook/templates/deployment.yaml
@@ -26,6 +26,7 @@ spec:
           image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
           imagePullPolicy: {{ .Values.image.pullPolicy }}
           args:
+            - --v=2
             - --tls-cert-file=/tls/tls.crt
             - --tls-private-key-file=/tls/tls.key
           env:
diff --git a/deploy/freedns-webhook/values.yaml b/deploy/freedns-webhook/values.yaml
index 6e3d97c..a15228f 100644
--- a/deploy/freedns-webhook/values.yaml
+++ b/deploy/freedns-webhook/values.yaml
@@ -14,7 +14,7 @@ certManager:
 
 image:
   repository: penguinade/cert-manager-webhook-freedns
-  tag: 2024.10.31.04
+  tag: 2024.11.02.04
   pullPolicy: IfNotPresent
 
 nameOverride: ""
diff --git a/freedns/freedns.go b/freedns/freedns.go
index 9ec48ff..55c771c 100755
--- a/freedns/freedns.go
+++ b/freedns/freedns.go
@@ -35,16 +35,6 @@ const URI_SUBDOMAIN_EDIT = "https://freedns.afraid.org/subdomain/edit.php?data_i
 const URI_LOGOUT = "https://freedns.afraid.org/logout/"
 const URI_DELETE_RECORD = "https://freedns.afraid.org/subdomain/delete2.php?data_id[]=%s&submit=delete%%20selected"
 
-func LogInfo(Mesg string) {
-	//fmt.Println(Mesg)
-	logf.V(logf.InfoLevel).Info(Mesg)
-}
-
-func LogDebug(Mesg string) {
-	//fmt.Println(Mesg)
-	logf.V(logf.DebugLevel).Info(Mesg)
-}
-
 func GetDomainFromZone(Zone string) string {
 	_segs := strings.Split(strings.TrimSuffix(Zone, "."), ".")
 	_segs = _segs[len(_segs)-2:]
@@ -165,7 +155,7 @@ loop:
 			break loop
 		case html.TextToken:
 			if inBold && strings.TrimSpace(htmlTokens.Token().Data) == DomainName {
-				LogInfo("Found " + DomainName + ", looking for domain id")
+				logf.V(logf.InfoLevel).Info(fmt.Sprintf("Found HTMLTextNode that contains \"%s\", try looking for domain id", DomainName))
 				lookForA = true
 			}
 			// The [Manage] anchor is next to the bold tag
@@ -180,7 +170,7 @@ loop:
 					_href := string(attrValue)
 					if string(attrKey) == "href" && strings.HasPrefix(_href, "/subdomain/?limit=") {
 						dnsObj.DomainId = strings.TrimPrefix(_href, "/subdomain/?limit=")
-						LogDebug(fmt.Sprintf("Domain id for \"%s\" is %s\n", DomainName, dnsObj.DomainId))
+						logf.V(logf.InfoLevel).Info(fmt.Sprintf("Domain id for \"%s\" is %s", DomainName, dnsObj.DomainId))
 						break loop
 					}
 					if !moreAttr {
@@ -202,6 +192,9 @@ func (dnsObj *FreeDNS) AddRecord(RecordType string, Subdomain string, Address st
 	if dnsObj.DomainId == "" {
 		return errors.New("No domain selected")
 	}
+
+	logf.V(logf.InfoLevel).Info(fmt.Sprintf("Adding %s Record: %s %s", RecordType, Subdomain, Address))
+
 	recordData := url.Values{}
 	recordData.Set("type", RecordType)
 	recordData.Set("domain_id", dnsObj.DomainId)
@@ -221,7 +214,7 @@ func (dnsObj *FreeDNS) AddRecord(RecordType string, Subdomain string, Address st
 
 		// Record already exists, treat this as success
 		if strings.Contains(respStr, "already have another already existent") {
-			LogInfo("Record already exists")
+			logf.V(logf.InfoLevel).Info("Record already exists")
 			return nil
 		}
 
@@ -281,7 +274,7 @@ func (dnsObj *FreeDNS) AddRecord(RecordType string, Subdomain string, Address st
 	}
 
 	if strings.HasPrefix(_Location.Path, "/zc.php") {
-		LogDebug("Error on AddRecord: Cookie expired")
+		logf.V(logf.DebugLevel).Info("Error on AddRecord: Cookie expired")
 		return errors.New("dns_cookie maybe expired")
 	}
 
@@ -289,6 +282,9 @@ func (dnsObj *FreeDNS) AddRecord(RecordType string, Subdomain string, Address st
 }
 
 func (dnsObj *FreeDNS) DeleteRecord(RecordId string) error {
+
+	logf.V(logf.InfoLevel).Info(fmt.Sprintf("(id=%s) Removing Record", RecordId))
+
 	resp, _, err := _HttpRequest("GET", fmt.Sprintf(URI_DELETE_RECORD, RecordId), nil, dnsObj.AuthCookie)
 	if err != nil {
 		return err
@@ -391,7 +387,7 @@ loop:
 	// Begin deep search for truncated records
 	htmlAddr := strings.ReplaceAll(html.EscapeString(Address), "&#34;", "&quot;")
 	for _, RecordId := range DeepSearchCandidates {
-		LogDebug("Searching in " + RecordId)
+		logf.V(logf.DebugLevel).Info("Searching in " + RecordId)
 		_, respStr, err := _HttpRequest("GET", URI_SUBDOMAIN_EDIT+RecordId, nil, dnsObj.AuthCookie)
 		if err != nil {
 			continue
diff --git a/main.go b/main.go
index 118abab..da33ecd 100644
--- a/main.go
+++ b/main.go
@@ -12,6 +12,7 @@ import (
 	"k8s.io/client-go/kubernetes"
 	"k8s.io/client-go/rest"
 
+	logf "github.com/cert-manager/cert-manager/pkg/logs"
 	"github.com/cert-manager/webhook-freedns/freedns"
 	"github.com/cert-manager/cert-manager/pkg/acme/webhook/apis/acme/v1alpha1"
 	"github.com/cert-manager/cert-manager/pkg/acme/webhook/cmd"
@@ -121,8 +122,6 @@ func (c *customDNSProviderSolver) Present(ch *v1alpha1.ChallengeRequest) error {
 	_zone = strings.TrimRight(_zone, ".")
 	_key := "\"" + ch.Key + "\""
 
-	freedns.LogInfo(fmt.Sprintf("ADD %s %s", _zone, _key))
-
 	err = dnsObj.AddRecord("TXT", _zone, _key, false, "")
 	if err != nil {
 		return err
@@ -150,7 +149,7 @@ func (c *customDNSProviderSolver) CleanUp(ch *v1alpha1.ChallengeRequest) error {
 	_key := "\"" + ch.Key + "\""
 	_id, err := c.freedns.FindRecord(_addr, "TXT", _key)
 
-	freedns.LogInfo(fmt.Sprintf("DEL %s %s", _addr, _key))
+	logf.V(logf.InfoLevel).Info(fmt.Sprintf("(id=%s) TXT Record: %s %s", _id, _addr, _key))
 
 	if _id != "" {
 		err = c.freedns.DeleteRecord(_id)