Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,12 @@ check: $(GOIMPORTS) $(GOLANGCI_LINT) $(HELM) ## Runs golangci-lint, gofmt/goimpo
@bash $(GARDENER_HACK_DIR)/check-charts.sh ./charts

# generate mock types for the following services from the SDK (space-separated list)
SDK_MOCK_SERVICES := dns

.PHONY: generate-mocks
generate-mocks: $(MOCKGEN)
@echo "Running $(MOCKGEN)"
@go mod download
@for service in $(SDK_MOCK_SERVICES); do \
INTERFACES=`go doc -all github.com/stackitcloud/stackit-sdk-go/services/$$service | grep '^type Api.* interface' | sed -n 's/^type \(.*\) interface.*/\1/p' | paste -sd,`,DefaultApi; \
$(MOCKGEN) -destination ./pkg/stackit/client/mock/$$service/$$service.go -package $$service github.com/stackitcloud/stackit-sdk-go/services/$$service $$INTERFACES; \
done

@$(MOCKGEN) -destination ./pkg/stackit/client/mock/dns/dns.go -package dns github.com/stackitcloud/stackit-sdk-go/services/dns/v1api DefaultAPI
@$(MOCKGEN) -destination ./pkg/stackit/client/mock/loadbalancer/loadbalancer.go -package loadbalancer -imports loadbalancerv2api=github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api github.com/stackitcloud/stackit-sdk-go/services/loadbalancer/v2api DefaultAPI
@$(MOCKGEN) -destination ./pkg/stackit/client/mock/iaas/iaas.go -package iaas -imports iaasv2api=github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api DefaultAPI

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/spf13/pflag v1.0.10
github.com/stackitcloud/stackit-sdk-go/core v0.23.0
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.14.1
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.6
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.7.0
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.11.0
github.com/stackitcloud/stackit-sdk-go/services/resourcemanager v0.21.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,8 @@ github.com/stackitcloud/stackit-sdk-go/core v0.23.0 h1:zPrOhf3Xe47rKRs1fg/AqKYUi
github.com/stackitcloud/stackit-sdk-go/core v0.23.0/go.mod h1:osMglDby4csGZ5sIfhNyYq1bS1TxIdPY88+skE/kkmI=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.14.1 h1:9qIh4vsE1qnSF1n/yf45PCpl5PoaGkFp8LmmIU4QDgg=
github.com/stackitcloud/stackit-sdk-go/services/authorization v0.14.1/go.mod h1:hZc0FvlEhN8zmlORsKLBjBep6iYAY/QLnoV1rDnY69A=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.6 h1:GBRb49x5Nax/oQQaaf2F3kKwv8DQQOL0TQOC0C/v/Ew=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.17.6/go.mod h1:IX9iL3MigDZUmzwswTJMfYvyi118KAHrFMfjJUy5NYk=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1 h1:VfszhFq/Snsd0LnflS8PbM0d9cG98hOFpamfjlcTnDQ=
github.com/stackitcloud/stackit-sdk-go/services/dns v0.19.1/go.mod h1:gBv6YkB3Xf3c0ZXg2GwtWY8zExwGPF/Ag114XiiERxg=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.7.0 h1:WyOC7tEazOPF9PwlExsycYvhm2l9OcjGQfZHDAckVIc=
github.com/stackitcloud/stackit-sdk-go/services/iaas v1.7.0/go.mod h1:78hDXs5sEokK2sAaBSn79kodytszmDYEAuMS56kPHIw=
github.com/stackitcloud/stackit-sdk-go/services/loadbalancer v1.11.0 h1:81q4OIt54bw4Of5UP/sqXTgT8LkzeY+uHIgSvzQQl3w=
Expand Down
59 changes: 39 additions & 20 deletions pkg/stackit/client/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"strings"

sdkconfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
dns "github.com/stackitcloud/stackit-sdk-go/services/dns/v1api"
"k8s.io/utils/set"

stackitv1alpha1 "github.com/stackitcloud/gardener-extension-provider-stackit/v2/pkg/apis/stackit/v1alpha1"
"github.com/stackitcloud/gardener-extension-provider-stackit/v2/pkg/stackit"
)

func NewDNSClient(ctx context.Context, endpoints stackitv1alpha1.APIEndpoints, credentials *stackit.Credentials) (DNSClient, error) {
func NewDNSClient(_ context.Context, endpoints stackitv1alpha1.APIEndpoints, credentials *stackit.Credentials) (DNSClient, error) {
options := clientOptions(endpoints, credentials)

if endpoints.DNS != nil {
Expand All @@ -25,7 +25,7 @@ func NewDNSClient(ctx context.Context, endpoints stackitv1alpha1.APIEndpoints, c
return nil, err
}
return &dnsClient{
api: apiClient,
api: apiClient.DefaultAPI,
projectID: credentials.ProjectID,
}, nil
}
Expand All @@ -42,23 +42,23 @@ type DNSZone struct {
}

type dnsClient struct {
api dns.DefaultApi
api dns.DefaultAPI

projectID string
}

func (c *dnsClient) ListZones(ctx context.Context) ([]DNSZone, error) {
dnsZonesResp, err := c.api.ListZonesExecute(ctx, c.projectID)
dnsZonesResp, err := c.api.ListZones(ctx, c.projectID).Execute()
if err != nil {
return nil, err
}

if dnsZonesResp == nil || dnsZonesResp.Zones == nil {
if dnsZonesResp == nil {
return []DNSZone{}, nil
}

result := make([]DNSZone, 0, len(*dnsZonesResp.Zones))
for _, zone := range *dnsZonesResp.Zones {
result := make([]DNSZone, 0, len(dnsZonesResp.Zones))
for _, zone := range dnsZonesResp.Zones {
result = append(result, DNSZone{
ID: zone.GetId(),
DNSName: zone.GetDnsName(),
Expand All @@ -76,35 +76,40 @@ func (c *dnsClient) CreateOrUpdateRecordSet(ctx context.Context,
return fmt.Errorf("failed to find record set: %w", err)
}

wantedRecordsPayload := []dns.RecordPayload{}
var wantedRecordsPayload []dns.RecordPayload
for _, record := range wantedRecords {
wantedRecordsPayload = append(wantedRecordsPayload, dns.RecordPayload{
Content: new(record),
Content: record,
})
}

cacheTTL, err := toCacheTTL(ttl)
if err != nil {
return err
}

if recordSet == nil {
_, err := c.api.CreateRecordSet(ctx, c.projectID, zoneID).CreateRecordSetPayload(dns.CreateRecordSetPayload{
Name: &name,
Records: &wantedRecordsPayload,
Type: new(dns.CreateRecordSetPayloadTypes(recordType)),
Ttl: new(ttl),
Name: name,
Records: wantedRecordsPayload,
Type: recordType,
Ttl: new(cacheTTL),
}).Execute()
if err != nil {
return fmt.Errorf("failed to create record set: %w", err)
}
return nil
}

if recordSet.GetTtl() == ttl && areRecordsEqual(recordSet.GetRecords(), wantedRecords) {
if recordSet.GetTtl() == cacheTTL && areRecordsEqual(recordSet.GetRecords(), wantedRecords) {
// If TTL and records are the same, no update is necessary
return nil
}

_, err = c.api.PartialUpdateRecordSet(ctx, c.projectID, zoneID, recordSet.GetId()).PartialUpdateRecordSetPayload(dns.PartialUpdateRecordSetPayload{
Name: &name,
Records: &wantedRecordsPayload,
Ttl: new(ttl),
Records: wantedRecordsPayload,
Ttl: new(cacheTTL),
}).Execute()
if err != nil {
return fmt.Errorf("failed to update record set: %w", err)
Expand All @@ -122,15 +127,15 @@ func (c *dnsClient) DeleteRecordSet(ctx context.Context, zoneID, name, recordTyp
return nil
}

_, err = c.api.DeleteRecordSetExecute(ctx, c.projectID, zoneID, recordSet.GetId())
_, err = c.api.DeleteRecordSet(ctx, c.projectID, zoneID, recordSet.GetId()).Execute()
if err != nil {
return fmt.Errorf("failed to delete record set: %w", err)
}
return nil
}

func (c *dnsClient) findRecordSet(ctx context.Context, zoneID, name, recordType string) (*dns.RecordSet, error) {
resp, err := c.api.ListRecordSetsExecute(ctx, c.projectID, zoneID)
resp, err := c.api.ListRecordSets(ctx, c.projectID, zoneID).Execute()
if err != nil {
return nil, err
}
Expand All @@ -143,7 +148,7 @@ func (c *dnsClient) findRecordSet(ctx context.Context, zoneID, name, recordType
if strings.TrimSuffix(recordSet.GetName(), ".") != name {
continue
}
if string(recordSet.GetType()) != recordType {
if recordSet.GetType() != recordType {
continue
}
return &recordSet, nil
Expand All @@ -163,3 +168,17 @@ func areRecordsEqual(existingRecords []dns.Record, newRecords []string) bool {

return existingRecordsSet.Equal(set.New(newRecords...))
}

// Validation based on https://docs.api.stackit.cloud/documentation/dns/version/v1#tag/record-set/operation/post-rrset
func toCacheTTL(i int64) (int32, error) {
if i < 60 {
return -1, fmt.Errorf("DNS CacheTTL cannot be lower than 60")
}

// 99999999 is lower than math.MaxInt32
if i > 99999999 {
return -1, fmt.Errorf("DNS CacheTTL cannot be higher than 99999999")
}

return int32(i), nil
}
Loading