go.ligato.io/vpp-agent/v3@v3.5.0/plugins/vpp/dnsplugin/descriptor/dnscache.go (about) 1 // Copyright (c) 2020 Pantheon.tech 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package descriptor 16 17 import ( 18 "net" 19 20 "github.com/go-errors/errors" 21 "go.ligato.io/cn-infra/v2/logging" 22 scheduler "go.ligato.io/vpp-agent/v3/plugins/kvscheduler/api" 23 "go.ligato.io/vpp-agent/v3/plugins/vpp/dnsplugin/descriptor/adapter" 24 "go.ligato.io/vpp-agent/v3/plugins/vpp/dnsplugin/vppcalls" 25 dns "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/dns" 26 ) 27 28 const ( 29 // DNSCacheDescriptorName is the name of the descriptor for VPP DNS cache server functionality 30 DNSCacheDescriptorName = "vpp-dns-cache" 31 ) 32 33 // DNSCacheDescriptor teaches KVScheduler how to configure VPP to act as DNS cache server. 34 type DNSCacheDescriptor struct { 35 // dependencies 36 log logging.Logger 37 dnsHandler vppcalls.DNSVppAPI 38 } 39 40 // NewDNSCacheDescriptor creates a new instance of the DNSCache descriptor. 41 func NewDNSCacheDescriptor(dnsHandler vppcalls.DNSVppAPI, log logging.PluginLogger) *scheduler.KVDescriptor { 42 ctx := &DNSCacheDescriptor{ 43 log: log.NewLogger("dnscache-descriptor"), 44 dnsHandler: dnsHandler, 45 } 46 47 typedDescr := &adapter.DNSCacheDescriptor{ 48 Name: DNSCacheDescriptorName, 49 KeySelector: dns.ModelDNSCache.IsKeyValid, 50 ValueTypeName: dns.ModelDNSCache.ProtoName(), 51 KeyLabel: dns.ModelDNSCache.StripKeyPrefix, 52 NBKeyPrefix: dns.ModelDNSCache.KeyPrefix(), 53 Validate: ctx.ValidateDNSCache, 54 Create: ctx.Create, 55 Delete: ctx.Delete, 56 } 57 return adapter.NewDNSCacheDescriptor(typedDescr) 58 } 59 60 // ValidateDNSCache validates content of DNS cache server configuration 61 func (d *DNSCacheDescriptor) ValidateDNSCache(key string, dnsCache *dns.DNSCache) error { 62 if len(dnsCache.UpstreamDnsServers) == 0 { 63 return scheduler.NewInvalidValueError( 64 errors.New("at least one upstream DNS server must be defined"), "upstreamDnsServers") 65 } 66 for _, serverIpAddress := range dnsCache.UpstreamDnsServers { 67 if net.ParseIP(serverIpAddress) == nil { 68 return scheduler.NewInvalidValueError(errors.Errorf("failed to parse upstream DNS Server IP "+ 69 "address %s, should be a valid ipv4/ipv6 address", serverIpAddress), "upstreamDnsServers") 70 } 71 } 72 return nil 73 } 74 75 // Create enables and configures DNS functionality in VPP using VPP's binary api 76 func (d *DNSCacheDescriptor) Create(key string, value *dns.DNSCache) (metadata interface{}, err error) { 77 for _, serverIPString := range value.UpstreamDnsServers { 78 // Note: net.ParseIP should be always successful thanks to validation 79 if err := d.dnsHandler.AddUpstreamDNSServer(net.ParseIP(serverIPString)); err != nil { 80 return nil, errors.Errorf("can't add upstream DNS server "+ 81 "with IP %s due to: %v", serverIPString, err) 82 } 83 } 84 if err := d.dnsHandler.EnableDNS(); err != nil { 85 return nil, errors.Errorf("failed to enable DNS due to: %v", err) 86 } 87 return nil, nil 88 } 89 90 // Delete disables (and removes configuration) DNS functionality in VPP using VPP's binary api 91 func (d *DNSCacheDescriptor) Delete(key string, value *dns.DNSCache, metadata interface{}) error { 92 if err := d.dnsHandler.DisableDNS(); err != nil { 93 return errors.Errorf("failed to disable DNS due to: %v", err) 94 } 95 for _, serverIPString := range value.UpstreamDnsServers { 96 // Note: net.ParseIP should be always successful thanks to validation 97 if err := d.dnsHandler.DeleteUpstreamDNSServer(net.ParseIP(serverIPString)); err != nil { 98 return errors.Errorf("can't remove upstream DNS server "+ 99 "with IP %s due to: %v", serverIPString, err) 100 } 101 } 102 return nil 103 }