github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/dind/pkg/cluster-up/options/options.go (about)

     1  /*
     2  Copyright 2018 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 options
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"net"
    23  )
    24  
    25  // Options holds information typically set by flags.
    26  type Options struct {
    27  	SideloadImage bool
    28  	DinDNodeImage string
    29  	ProxyAddr     string
    30  	Version       string
    31  	NumNodes      int
    32  }
    33  
    34  // New takes a FlagSet, parses it, and creates an Options struct. This calls Parse, so add any external flags before creating an Options object.
    35  func New(set *flag.FlagSet, args []string) (*Options, error) {
    36  	o := Options{}
    37  
    38  	set.BoolVar(&o.SideloadImage, "side-load-image", true, "If the image needs to be side-loaded from the file-system.")
    39  	set.StringVar(&o.DinDNodeImage, "dind-node-image", "k8s.gcr.io/dind-node-amd64", "The dind node image to use.")
    40  	set.StringVar(&o.ProxyAddr, "proxy-addr", "", "The externally facing address for kubeadm to add to SAN.")
    41  	set.StringVar(&o.Version, "k8s-version", "", "The kubernetes version to spin up.")
    42  	set.IntVar(&o.NumNodes, "num-nodes", 4, "The number of nodes to make, including the master if applicable.")
    43  
    44  	if err := set.Parse(args); err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	if err := o.validate(); err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	return &o, nil
    53  }
    54  
    55  func (o *Options) validate() error {
    56  	if o.NumNodes < 1 {
    57  		return fmt.Errorf("Must provide at least 1 node, got %d", o.NumNodes)
    58  	}
    59  
    60  	// The ParseIP function returns a nil if it's not a valid ipv4 or ipv6 address.
    61  	ip := net.ParseIP(o.ProxyAddr)
    62  	if o.ProxyAddr != "" && ip == nil {
    63  		return fmt.Errorf("the external proxy %q is not a valid ip address", o.ProxyAddr)
    64  	}
    65  
    66  	return nil
    67  }