github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/dind/cmd/cluster-up/main.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 main
    18  
    19  import (
    20  	"bufio"
    21  	"flag"
    22  	"fmt"
    23  	"net"
    24  	"os"
    25  	"strings"
    26  	"time"
    27  
    28  	"github.com/golang/glog"
    29  	"k8s.io/test-infra/dind/pkg/cluster-up/cluster"
    30  	"k8s.io/test-infra/dind/pkg/cluster-up/options"
    31  )
    32  
    33  const (
    34  	masterConfigDir = "/var/kubernetes"
    35  	kubecfg         = "/var/kubernetes/admin.conf"
    36  	timeout         = time.Duration(5) * time.Minute
    37  )
    38  
    39  // dockerImageVersion reads the version from the metadata stored in the container.
    40  func dockerImageVersion() (string, error) {
    41  	f, err := os.Open("/docker_version")
    42  	if err != nil {
    43  		return "", err
    44  	}
    45  	defer f.Close()
    46  	scanner := bufio.NewScanner(f)
    47  	scanner.Split(bufio.ScanLines)
    48  	for scanner.Scan() {
    49  		return scanner.Text(), nil
    50  	}
    51  	return "", fmt.Errorf("empty version file")
    52  }
    53  
    54  func nonLocalUnicastAddr(intfName string) (string, error) {
    55  	// Finds a non-localhost unicast address on the given interface.
    56  	intf, err := net.InterfaceByName(intfName)
    57  	if err != nil {
    58  		return "", err
    59  	}
    60  	addrs, err := intf.Addrs()
    61  	if err != nil {
    62  		return "", err
    63  	}
    64  	for _, addr := range addrs {
    65  		if !strings.HasPrefix("127", addr.String()) {
    66  			ip, _, err := net.ParseCIDR(addr.String())
    67  			if err != nil {
    68  				return "", err
    69  			}
    70  
    71  			return ip.String(), nil
    72  		}
    73  	}
    74  
    75  	return "", fmt.Errorf("no non-localhost unicast addresses found on interface %s", intfName)
    76  }
    77  
    78  func main() {
    79  	// Discard the error, because flag.CommandLine is already set to ExitOnError.
    80  	o, _ := options.New(flag.CommandLine, os.Args[1:])
    81  	defer glog.Flush()
    82  
    83  	// Dynamically lookup any arguments that weren't provided.
    84  	if o.Version == "" {
    85  		v, err := dockerImageVersion()
    86  		if err != nil {
    87  			glog.Fatalf("Failed to load docker image version: %v", err)
    88  		}
    89  		o.Version = v
    90  	}
    91  
    92  	if o.ProxyAddr == "" {
    93  		p, err := nonLocalUnicastAddr("eth0")
    94  		if err != nil {
    95  			glog.Fatalf("Failed to determine external proxy address.")
    96  		}
    97  		o.ProxyAddr = p
    98  	}
    99  	glog.Infof("External proxy for alt SAN is %s", o.ProxyAddr)
   100  
   101  	// Create a cluster, and wait for it to come up.
   102  	c, err := cluster.New(o.NumNodes, o.ProxyAddr, o.DinDNodeImage, "/var/kubernetes", "testnet", "/dind-node-bundle.tar", o.Version, timeout)
   103  
   104  	if err != nil {
   105  		glog.Fatalf("Failed to create a dind cluster object: %v", err)
   106  	}
   107  	glog.Infof("Starting a cluster.")
   108  	if err := c.Up(o.SideloadImage); err != nil {
   109  		glog.Fatalf("Failed to wait for cluster to come up: %v", err)
   110  	}
   111  	glog.Infof("Cluster is initialized.")
   112  }