/
util.go
100 lines (88 loc) · 3.19 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2020 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package config
import (
"context"
"os"
"time"
"golang.org/x/oauth2/google"
"google.golang.org/api/dns/v1"
"google.golang.org/api/option"
"k8s.io/apimachinery/pkg/util/wait"
)
// EnvConfig is the config parsed from environment variables by envconfig.
type EnvConfig struct {
FullHostName string `envconfig:"full_host_name" required:"true"`
DomainName string `envconfig:"domain_name" required:"true"`
DNSZone string `envconfig:"dns_zone" required:"true"`
CloudDNSServiceAccountKeyFile string `envconfig:"cloud_dns_service_account_key_file" required:"true"`
CloudDNSProject string `envconfig:"cloud_dns_project" required:"true"`
IngressIP string `envconfig:"ingress_ip" required:"true"`
}
// DNSRecord represents an IP and Domain.
type DNSRecord struct {
IP string
Domain string
}
// MakeRecordSet creates a dns.ResourceRecordSet for a DNSRecord.
func MakeRecordSet(record *DNSRecord) *dns.ResourceRecordSet {
dnsName := record.Domain + "."
return &dns.ResourceRecordSet{
Name: dnsName,
Rrdatas: []string{record.IP},
// Setting TTL of DNS record to 5 seconds to make DNS become effective more quickly.
Ttl: int64(5),
Type: "A",
}
}
// DeleteDNSRecord deletes the given DNS record.
func DeleteDNSRecord(record *DNSRecord, svcAccountKeyFile, dnsProject, dnsZone string) error {
rec := MakeRecordSet(record)
svc, err := GetCloudDNSSvc(svcAccountKeyFile)
if err != nil {
return err
}
deletion := &dns.Change{
Deletions: []*dns.ResourceRecordSet{rec},
}
return ChangeDNSRecord(deletion, svc, dnsProject, dnsZone)
}
// ChangeDNSRecord changes the given DNS record.
func ChangeDNSRecord(change *dns.Change, svc *dns.Service, dnsProject, dnsZone string) error {
chg, err := svc.Changes.Create(dnsProject, dnsZone, change).Do()
if err != nil {
return err
}
// Wait for change to be acknowledged.
return wait.PollImmediate(10*time.Second, 5*time.Minute, func() (bool, error) {
tmp, err := svc.Changes.Get(dnsProject, dnsZone, chg.Id).Do()
if err != nil {
return false, err
}
return tmp.Status != "pending", nil
})
}
// GetCloudDNSSvc returns the Cloud DNS Service stub.
// reference: https://github.com/jetstack/cert-manager/blob/master/pkg/issuer/acme/dns/clouddns/clouddns.go
func GetCloudDNSSvc(svcAccountKeyFile string) (*dns.Service, error) {
data, err := os.ReadFile(svcAccountKeyFile)
if err != nil {
return nil, err
}
conf, err := google.JWTConfigFromJSON(data, dns.NdevClouddnsReadwriteScope)
if err != nil {
return nil, err
}
ctx := context.Background()
return dns.NewService(ctx, option.WithHTTPClient(conf.Client(ctx)))
}