"Fossies" - the Fresh Open Source Software Archive

Member "istio-1.6.5/mixer/tools/mixgen/cmd/adapter.go" (8 Jul 2020, 5023 Bytes) of package /linux/misc/istio-1.6.5.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 // Copyright 2018 Istio Authors
    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 cmd
   16 
   17 import (
   18     "bytes"
   19     "encoding/base64"
   20     "fmt"
   21     "io/ioutil"
   22     "os"
   23     "path"
   24     "path/filepath"
   25     "strings"
   26     gotemplate "text/template"
   27 
   28     "github.com/gogo/protobuf/proto"
   29     "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
   30     "github.com/spf13/cobra"
   31 
   32     "istio.io/istio/mixer/cmd/shared"
   33     "istio.io/istio/mixer/pkg/runtime/config/constant"
   34     "istio.io/istio/pkg/test/env"
   35 )
   36 
   37 func adapterCfgCmd(rawArgs []string, printf, fatalf shared.FormatFn) *cobra.Command {
   38     var resName string
   39     var ns string
   40     var configFilePath string
   41     var output string
   42     var description string
   43     var sessionBased bool
   44     var templates []string
   45 
   46     adapterCmd := &cobra.Command{
   47         Use:   "adapter",
   48         Short: "creates kubernetes configuration for an adapter",
   49         Run: func(cmd *cobra.Command, args []string) {
   50             createAdapterCr("mixgen "+strings.Join(rawArgs, " "), resName, ns, description, configFilePath,
   51                 sessionBased, templates, output, printf, fatalf)
   52         },
   53     }
   54     adapterCmd.PersistentFlags().StringVarP(&resName, "name", "n", "", "name of the resource")
   55     adapterCmd.PersistentFlags().StringVar(&ns, "namespace", constant.DefaultConfigNamespace, "namespace of the resource")
   56     adapterCmd.PersistentFlags().StringVarP(&description, "description", "d", "",
   57         "description of the adapter")
   58     adapterCmd.PersistentFlags().StringVarP(&configFilePath, "config", "c", "",
   59         "path to the adapter config's protobuf file descriptor set file "+
   60             "(protobuf file descriptor set is created using `protoc -o <path to adapter config proto file> <Flags>`)")
   61     adapterCmd.PersistentFlags().BoolVarP(&sessionBased, "session_based", "s", true,
   62         "whether the adapter is session based or not. TODO link to the documentation")
   63     adapterCmd.PersistentFlags().StringArrayVarP(&templates, "templates", "t", nil,
   64         "supported template names")
   65     adapterCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "output file path"+
   66         " to save the configuration; if it a directory the generate file name is <dir name>/<adapter name>.yaml")
   67     return adapterCmd
   68 }
   69 
   70 func createAdapterCr(rawCommand string, name, namespace, description, config string, sessionBased bool, templates []string,
   71     outPath string, printf, fatalf shared.FormatFn) {
   72     type adapterCRVar struct {
   73         RawCommand   string
   74         Name         string
   75         Namespace    string
   76         Description  string
   77         Config       string
   78         SessionBased bool
   79         Templates    []string
   80     }
   81     adapterTmpl := `# this config is created through command
   82 # {{.RawCommand}}
   83 apiVersion: "config.istio.io/v1alpha2"
   84 kind: adapter
   85 metadata:
   86   name: {{.Name}}
   87   namespace: {{.Namespace}}
   88 spec:
   89   description: {{.Description}}
   90   session_based: {{.SessionBased}}
   91   templates:
   92   {{range .Templates -}}
   93   - {{.}}
   94   {{end -}}
   95   config: {{.Config}}
   96 ---
   97 `
   98 
   99     var byts []byte
  100     var err error
  101 
  102     if config != "" {
  103         // no config means adapter has no config.
  104         inPath, _ := filepath.Abs(config)
  105         byts, err = ioutil.ReadFile(inPath)
  106         if err != nil {
  107             fatalf("unable to read file %s. %v", inPath, err)
  108         }
  109         // validate if the file is a file descriptor set with imports.
  110         if err = isFds(byts); err != nil {
  111             fatalf("config in invalid: %v", err)
  112         }
  113     }
  114 
  115     adapterObj := &adapterCRVar{
  116         RawCommand:   strings.Replace(rawCommand, env.IstioSrc, "$REPO_ROOT", -1),
  117         Name:         name,
  118         Namespace:    namespace,
  119         Description:  description,
  120         Config:       base64.StdEncoding.EncodeToString(byts),
  121         SessionBased: sessionBased,
  122         Templates:    templates,
  123     }
  124 
  125     t := gotemplate.New("adaptercr")
  126     w := &bytes.Buffer{}
  127     t, _ = t.Parse(adapterTmpl)
  128     if err = t.Execute(w, adapterObj); err != nil {
  129         fatalf("could not create adapter custom resource" + err.Error())
  130     }
  131 
  132     if outPath != "" {
  133         var s os.FileInfo
  134         if s, err = os.Stat(outPath); err != nil {
  135             fatalf("cannot write to output file '%s': %v", outPath, err)
  136         }
  137 
  138         if s.IsDir() {
  139             outPath = path.Join(outPath, adapterObj.Name+".yaml")
  140         }
  141 
  142         if err = ioutil.WriteFile(outPath, w.Bytes(), 0644); err != nil {
  143             fatalf("cannot write to output file '%s': %v", outPath, err)
  144         }
  145     } else {
  146         printf(w.String())
  147     }
  148 }
  149 
  150 func isFds(byts []byte) error {
  151     fds := &descriptor.FileDescriptorSet{}
  152     err := proto.Unmarshal(byts, fds)
  153     if err != nil {
  154         return err
  155     }
  156 
  157     if len(fds.File) == 1 && len(fds.File[0].Dependency) > 0 {
  158         // fds is created without --include_imports.
  159         return fmt.Errorf("the file descriptor set was created without including imports" +
  160             ". Please run protoc with `--include_imports` flag")
  161     }
  162     return nil
  163 }