|
| 1 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +// you may not use this file except in compliance with the License. |
| 3 | +// You may obtain a copy of the License at |
| 4 | +// |
| 5 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// |
| 7 | +// Unless required by applicable law or agreed to in writing, software |
| 8 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +// See the License for the specific language governing permissions and |
| 11 | +// limitations under the License. |
| 12 | + |
| 13 | +package scaffold |
| 14 | + |
| 15 | +import ( |
| 16 | + "bytes" |
| 17 | + "context" |
| 18 | + "encoding/json" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + "time" |
| 22 | + |
| 23 | + adctypes "github.com/apache/apisix-ingress-controller/api/adc" |
| 24 | + "github.com/apache/apisix-ingress-controller/internal/provider/adc/translator" |
| 25 | +) |
| 26 | + |
| 27 | +// DataplaneResource defines the interface for accessing dataplane resources |
| 28 | +type DataplaneResource interface { |
| 29 | + Route() RouteResource |
| 30 | + Service() ServiceResource |
| 31 | + SSL() SSLResource |
| 32 | + Consumer() ConsumerResource |
| 33 | +} |
| 34 | + |
| 35 | +// RouteResource defines the interface for route resources |
| 36 | +type RouteResource interface { |
| 37 | + List(ctx context.Context) ([]*adctypes.Route, error) |
| 38 | +} |
| 39 | + |
| 40 | +// ServiceResource defines the interface for service resources |
| 41 | +type ServiceResource interface { |
| 42 | + List(ctx context.Context) ([]*adctypes.Service, error) |
| 43 | +} |
| 44 | + |
| 45 | +// SSLResource defines the interface for SSL resources |
| 46 | +type SSLResource interface { |
| 47 | + List(ctx context.Context) ([]*adctypes.SSL, error) |
| 48 | +} |
| 49 | + |
| 50 | +// ConsumerResource defines the interface for consumer resources |
| 51 | +type ConsumerResource interface { |
| 52 | + List(ctx context.Context) ([]*adctypes.Consumer, error) |
| 53 | +} |
| 54 | + |
| 55 | +// adcDataplaneResource implements DataplaneResource using ADC command |
| 56 | +type adcDataplaneResource struct { |
| 57 | + backend string |
| 58 | + serverAddr string |
| 59 | + token string |
| 60 | + tlsVerify bool |
| 61 | + syncTimeout time.Duration |
| 62 | +} |
| 63 | + |
| 64 | +// newADCDataplaneResource creates a new ADC-based dataplane resource |
| 65 | +func newADCDataplaneResource(backend, serverAddr, token string, tlsVerify bool) DataplaneResource { |
| 66 | + return &adcDataplaneResource{ |
| 67 | + backend: backend, |
| 68 | + serverAddr: serverAddr, |
| 69 | + token: token, |
| 70 | + tlsVerify: tlsVerify, |
| 71 | + syncTimeout: 30 * time.Second, |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (a *adcDataplaneResource) Route() RouteResource { |
| 76 | + return &adcRouteResource{a} |
| 77 | +} |
| 78 | + |
| 79 | +func (a *adcDataplaneResource) Service() ServiceResource { |
| 80 | + return &adcServiceResource{a} |
| 81 | +} |
| 82 | + |
| 83 | +func (a *adcDataplaneResource) SSL() SSLResource { |
| 84 | + return &adcSSLResource{a} |
| 85 | +} |
| 86 | + |
| 87 | +func (a *adcDataplaneResource) Consumer() ConsumerResource { |
| 88 | + return &adcConsumerResource{a} |
| 89 | +} |
| 90 | + |
| 91 | +// dumpResources executes adc dump command and returns the resources |
| 92 | +func (a *adcDataplaneResource) dumpResources(ctx context.Context) (*translator.TranslateResult, error) { |
| 93 | + ctxWithTimeout, cancel := context.WithTimeout(ctx, a.syncTimeout) |
| 94 | + defer cancel() |
| 95 | + |
| 96 | + args := []string{"dump", "-o", "/tmp/dump.yaml"} |
| 97 | + if !a.tlsVerify { |
| 98 | + args = append(args, "--tls-skip-verify") |
| 99 | + } |
| 100 | + |
| 101 | + adcEnv := []string{ |
| 102 | + "ADC_BACKEND=" + a.backend, |
| 103 | + "ADC_SERVER=" + a.serverAddr, |
| 104 | + "ADC_TOKEN=" + a.token, |
| 105 | + } |
| 106 | + |
| 107 | + var stdout, stderr bytes.Buffer |
| 108 | + cmd := exec.CommandContext(ctxWithTimeout, "adc", args...) |
| 109 | + cmd.Stdout = &stdout |
| 110 | + cmd.Stderr = &stderr |
| 111 | + cmd.Env = append(cmd.Env, os.Environ()...) |
| 112 | + cmd.Env = append(cmd.Env, adcEnv...) |
| 113 | + |
| 114 | + if err := cmd.Run(); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + var resources adctypes.Resources |
| 119 | + if err := json.Unmarshal(stdout.Bytes(), &resources); err != nil { |
| 120 | + return nil, err |
| 121 | + } |
| 122 | + |
| 123 | + // Extract routes from services |
| 124 | + var routes []*adctypes.Route |
| 125 | + for _, service := range resources.Services { |
| 126 | + routes = append(routes, service.Routes...) |
| 127 | + } |
| 128 | + |
| 129 | + return &translator.TranslateResult{ |
| 130 | + Routes: routes, |
| 131 | + Services: resources.Services, |
| 132 | + SSL: resources.SSLs, |
| 133 | + GlobalRules: resources.GlobalRules, |
| 134 | + PluginMetadata: resources.PluginMetadata, |
| 135 | + Consumers: resources.Consumers, |
| 136 | + }, nil |
| 137 | +} |
| 138 | + |
| 139 | +// adcRouteResource implements RouteResource |
| 140 | +type adcRouteResource struct { |
| 141 | + *adcDataplaneResource |
| 142 | +} |
| 143 | + |
| 144 | +func (r *adcRouteResource) List(ctx context.Context) ([]*adctypes.Route, error) { |
| 145 | + result, err := r.dumpResources(ctx) |
| 146 | + if err != nil { |
| 147 | + return nil, err |
| 148 | + } |
| 149 | + return result.Routes, nil |
| 150 | +} |
| 151 | + |
| 152 | +// adcServiceResource implements ServiceResource |
| 153 | +type adcServiceResource struct { |
| 154 | + *adcDataplaneResource |
| 155 | +} |
| 156 | + |
| 157 | +func (s *adcServiceResource) List(ctx context.Context) ([]*adctypes.Service, error) { |
| 158 | + result, err := s.dumpResources(ctx) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + return result.Services, nil |
| 163 | +} |
| 164 | + |
| 165 | +// adcSSLResource implements SSLResource |
| 166 | +type adcSSLResource struct { |
| 167 | + *adcDataplaneResource |
| 168 | +} |
| 169 | + |
| 170 | +func (s *adcSSLResource) List(ctx context.Context) ([]*adctypes.SSL, error) { |
| 171 | + result, err := s.dumpResources(ctx) |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + return result.SSL, nil |
| 176 | +} |
| 177 | + |
| 178 | +// adcConsumerResource implements ConsumerResource |
| 179 | +type adcConsumerResource struct { |
| 180 | + *adcDataplaneResource |
| 181 | +} |
| 182 | + |
| 183 | +func (c *adcConsumerResource) List(ctx context.Context) ([]*adctypes.Consumer, error) { |
| 184 | + result, err := c.dumpResources(ctx) |
| 185 | + if err != nil { |
| 186 | + return nil, err |
| 187 | + } |
| 188 | + return result.Consumers, nil |
| 189 | +} |
0 commit comments