From 2dbc6dce7127d6abafac8aaab5affb3ffe8353ef Mon Sep 17 00:00:00 2001 From: HuangWei Date: Fri, 24 May 2024 14:13:09 +0800 Subject: [PATCH 1/3] try to zone one by one --- .gitignore | 1 + alidns/client.go | 31 ++++++++++++++++++++----------- alidns/solver.go | 6 +++--- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 6d02acc..4982aac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ vendor/ kubebuilder/ +.idea/ diff --git a/alidns/client.go b/alidns/client.go index f9a3a43..611e24b 100644 --- a/alidns/client.go +++ b/alidns/client.go @@ -23,22 +23,31 @@ func newClient(region string, cred auth.Credential) (*Client, error) { return &Client{dnsc: client}, nil } -func (c *Client) getHostedZone(zone string) (string, error) { +func (c *Client) getHostedZoneByFqdn(fqdn string) (string, error) { request := alidns.CreateDescribeDomainsRequest() - request.KeyWord = util.UnFqdn(zone) - request.SearchMode = "EXACT" - response, err := c.dnsc.DescribeDomains(request) - if err != nil { - return "", err - } + domain := util.UnFqdn(fqdn) + for i := len(domain) - 2; i >= 0; i-- { + if domain[i] != '.' { + continue + } + request.KeyWord = domain[i+1:] + request.SearchMode = "EXACT" + + response, err := c.dnsc.DescribeDomains(request) + if err != nil { + return "", err + } + + zones := response.Domains.Domain + if len(zones) == 0 { + continue + } - zones := response.Domains.Domain - if len(zones) == 0 { - return "", fmt.Errorf("zone %s does not exist", zone) + return zones[0].DomainName, nil } - return zones[0].DomainName, nil + return "", fmt.Errorf("cloud not find zone for: %s", fqdn) } func (c *Client) addTxtRecord(zone, rr, value string) error { diff --git a/alidns/solver.go b/alidns/solver.go index ec53609..343395d 100644 --- a/alidns/solver.go +++ b/alidns/solver.go @@ -52,7 +52,7 @@ func (s *Solver) Present(ch *v1alpha1.ChallengeRequest) error { return err } - zoneName, err := client.getHostedZone(ch.ResolvedZone) + zoneName, err := client.getHostedZoneByFqdn(ch.ResolvedZone) if err != nil { klog.Errorf("Get hosted zone %v error: %v", ch.ResolvedZone, err) return err @@ -104,7 +104,7 @@ func (s *Solver) getCredential(cfg *Config, ns string) (*credentials.AccessKeyCr } func (s *Solver) getSecretData(selector cmmetav1.SecretKeySelector, ns string) ([]byte, error) { - secret, err := s.client.CoreV1().Secrets(ns).Get(context.TODO(),selector.Name, metav1.GetOptions{}) + secret, err := s.client.CoreV1().Secrets(ns).Get(context.TODO(), selector.Name, metav1.GetOptions{}) if err != nil { return nil, errors.Wrapf(err, "failed to load secret %q", ns+"/"+selector.Name) } @@ -130,7 +130,7 @@ func (s *Solver) CleanUp(ch *v1alpha1.ChallengeRequest) error { return err } - zoneName, err := client.getHostedZone(ch.ResolvedZone) + zoneName, err := client.getHostedZoneByFqdn(ch.ResolvedFQDN) if err != nil { klog.Errorf("Get hosted zone %v error: %v", ch.ResolvedZone, err) return err From be788224c892a2f16bd96fada9175858135ede48 Mon Sep 17 00:00:00 2001 From: HuangWei Date: Fri, 24 May 2024 14:44:33 +0800 Subject: [PATCH 2/3] fix bug --- alidns/solver.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/alidns/solver.go b/alidns/solver.go index 343395d..c5c1b09 100644 --- a/alidns/solver.go +++ b/alidns/solver.go @@ -52,9 +52,9 @@ func (s *Solver) Present(ch *v1alpha1.ChallengeRequest) error { return err } - zoneName, err := client.getHostedZoneByFqdn(ch.ResolvedZone) + zoneName, err := client.getHostedZoneByFqdn(ch.ResolvedFQDN) if err != nil { - klog.Errorf("Get hosted zone %v error: %v", ch.ResolvedZone, err) + klog.Errorf("Get hosted fqdn %v error: %v", ch.ResolvedFQDN, err) return err } From 82aa4dd8295a232355196ce5abd7412f236f4a53 Mon Sep 17 00:00:00 2001 From: HuangWei Date: Fri, 24 May 2024 15:12:23 +0800 Subject: [PATCH 3/3] fix fqdn is zone --- alidns/client.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/alidns/client.go b/alidns/client.go index 611e24b..5373270 100644 --- a/alidns/client.go +++ b/alidns/client.go @@ -26,7 +26,8 @@ func newClient(region string, cred auth.Credential) (*Client, error) { func (c *Client) getHostedZoneByFqdn(fqdn string) (string, error) { request := alidns.CreateDescribeDomainsRequest() - domain := util.UnFqdn(fqdn) + // in case fqdn is zone + domain := "." + util.UnFqdn(fqdn) for i := len(domain) - 2; i >= 0; i-- { if domain[i] != '.' { continue