sigs.k8s.io/external-dns@v0.14.1/provider/plural/plural.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package plural 18 19 import ( 20 "context" 21 "fmt" 22 "os" 23 24 log "github.com/sirupsen/logrus" 25 26 "sigs.k8s.io/external-dns/endpoint" 27 "sigs.k8s.io/external-dns/plan" 28 "sigs.k8s.io/external-dns/provider" 29 ) 30 31 const ( 32 CreateAction = "c" 33 DeleteAction = "d" 34 ) 35 36 type PluralProvider struct { 37 provider.BaseProvider 38 Client Client 39 } 40 41 type RecordChange struct { 42 Action string 43 Record *DnsRecord 44 } 45 46 func NewPluralProvider(cluster, provider string) (*PluralProvider, error) { 47 token := os.Getenv("PLURAL_ACCESS_TOKEN") 48 endpoint := os.Getenv("PLURAL_ENDPOINT") 49 50 if token == "" { 51 return nil, fmt.Errorf("No plural access token provided, you must set the PLURAL_ACCESS_TOKEN env var") 52 } 53 54 config := &Config{ 55 Token: token, 56 Endpoint: endpoint, 57 Cluster: cluster, 58 Provider: provider, 59 } 60 61 client, err := NewClient(config) 62 if err != nil { 63 return nil, err 64 } 65 66 prov := &PluralProvider{ 67 Client: client, 68 } 69 70 return prov, nil 71 } 72 73 func (p *PluralProvider) Records(_ context.Context) (endpoints []*endpoint.Endpoint, err error) { 74 records, err := p.Client.DnsRecords() 75 if err != nil { 76 return 77 } 78 79 endpoints = make([]*endpoint.Endpoint, len(records)) 80 for i, record := range records { 81 endpoints[i] = endpoint.NewEndpoint(record.Name, record.Type, record.Records...) 82 } 83 return 84 } 85 86 func (p *PluralProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error) { 87 return endpoints, nil 88 } 89 90 func (p *PluralProvider) ApplyChanges(_ context.Context, diffs *plan.Changes) error { 91 var changes []*RecordChange 92 for _, endpoint := range diffs.Create { 93 changes = append(changes, makeChange(CreateAction, endpoint.Targets, endpoint)) 94 } 95 96 for _, desired := range diffs.UpdateNew { 97 changes = append(changes, makeChange(CreateAction, desired.Targets, desired)) 98 } 99 100 for _, deleted := range diffs.Delete { 101 changes = append(changes, makeChange(DeleteAction, []string{}, deleted)) 102 } 103 104 return p.applyChanges(changes) 105 } 106 107 func makeChange(change string, target []string, endpoint *endpoint.Endpoint) *RecordChange { 108 return &RecordChange{ 109 Action: change, 110 Record: &DnsRecord{ 111 Name: endpoint.DNSName, 112 Type: endpoint.RecordType, 113 Records: target, 114 }, 115 } 116 } 117 118 func (p *PluralProvider) applyChanges(changes []*RecordChange) error { 119 for _, change := range changes { 120 logFields := log.Fields{ 121 "name": change.Record.Name, 122 "type": change.Record.Type, 123 "action": change.Action, 124 } 125 log.WithFields(logFields).Info("Changing record.") 126 127 if change.Action == CreateAction { 128 _, err := p.Client.CreateRecord(change.Record) 129 if err != nil { 130 return err 131 } 132 } 133 if change.Action == DeleteAction { 134 if err := p.Client.DeleteRecord(change.Record.Name, change.Record.Type); err != nil { 135 return err 136 } 137 } 138 } 139 140 return nil 141 }