istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/test/framework/components/echo/deployment/flags.go (about)

     1  // Copyright 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 deployment
    16  
    17  import (
    18  	"bytes"
    19  	"flag"
    20  	"fmt"
    21  	"os"
    22  
    23  	"gopkg.in/yaml.v3"
    24  
    25  	"istio.io/istio/pkg/test/framework/components/echo"
    26  	"istio.io/istio/pkg/test/framework/config"
    27  	"istio.io/istio/pkg/test/util/file"
    28  )
    29  
    30  var additionalConfigs = &configs{}
    31  
    32  func init() {
    33  	flag.Var(additionalConfigs, "istio.test.echo.configs", "The path to a file containing a list of "+
    34  		"echo.Config in YAML which will be added to the set of echos for every suite.")
    35  }
    36  
    37  // configs wraps a slice of echo.Config to implement the config.Value interface, allowing
    38  // it to be configured by a flag, or within the test framework config file.
    39  type configs []echo.Config
    40  
    41  var _ config.Value = &configs{}
    42  
    43  func (c *configs) String() string {
    44  	buf := &bytes.Buffer{}
    45  	for _, cc := range *c {
    46  		_, _ = fmt.Fprintf(buf, "FQDN:     %s\n", cc.ClusterLocalFQDN())
    47  		_, _ = fmt.Fprintf(buf, "Headless: %v\n", cc.Headless)
    48  		_, _ = fmt.Fprintf(buf, "VM:       %v\n", cc.DeployAsVM)
    49  		if cc.DeployAsVM {
    50  			_, _ = fmt.Fprintf(buf, "VMDistro: %s\n", cc.VMDistro)
    51  		}
    52  		if cc.Cluster.Name() != "" {
    53  			_, _ = fmt.Fprintf(buf, "Cluster:  %s\n", cc.Cluster.Name())
    54  		}
    55  	}
    56  	return buf.String()
    57  }
    58  
    59  func (c *configs) Set(path string) error {
    60  	path, err := file.NormalizePath(path)
    61  	if err != nil {
    62  		return err
    63  	}
    64  	yml, err := os.ReadFile(path)
    65  	if err != nil {
    66  		return err
    67  	}
    68  	out, err := echo.ParseConfigs(yml)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	*c = out
    73  	return nil
    74  }
    75  
    76  func (c *configs) SetConfig(m any) error {
    77  	yml, err := yaml.Marshal(m)
    78  	if err != nil {
    79  		return err
    80  	}
    81  	out, err := echo.ParseConfigs(yml)
    82  	if err != nil {
    83  		return err
    84  	}
    85  	*c = out
    86  	return nil
    87  }