Exemplo n.º 1
0
func (o *ProbeOptions) Complete(f *clientcmd.Factory, cmd *cobra.Command, args []string) error {
	resources := args
	if i := cmd.ArgsLenAtDash(); i != -1 {
		resources = args[:i]
		o.Command = args[i:]
	}
	if len(o.Filenames) == 0 && len(args) < 1 {
		return kcmdutil.UsageError(cmd, "one or more resources must be specified as <resource> <name> or <resource>/<name>")
	}

	cmdNamespace, explicit, err := f.DefaultNamespace()
	if err != nil {
		return err
	}

	clientConfig, err := f.ClientConfig()
	if err != nil {
		return err
	}

	o.OutputVersion, err = kcmdutil.OutputVersion(cmd, clientConfig.GroupVersion)
	if err != nil {
		return err
	}

	mapper, typer := f.Object(false)
	o.Builder = resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), kapi.Codecs.UniversalDecoder()).
		ContinueOnError().
		NamespaceParam(cmdNamespace).DefaultNamespace().
		FilenameParam(explicit, false, o.Filenames...).
		SelectorParam(o.Selector).
		ResourceTypeOrNameArgs(o.All, resources...).
		Flatten()

	output := kcmdutil.GetFlagString(cmd, "output")
	if len(output) != 0 {
		o.PrintObject = func(obj runtime.Object) error { return f.PrintObject(cmd, mapper, obj, o.Out) }
	}

	o.Encoder = f.JSONEncoder()
	o.UpdatePodSpecForObject = f.UpdatePodSpecForObject
	o.ShortOutput = kcmdutil.GetFlagString(cmd, "output") == "name"
	o.Mapper = mapper

	if !cmd.Flags().Lookup("initial-delay-seconds").Changed {
		o.InitialDelaySeconds = nil
	}
	if !cmd.Flags().Lookup("timeout-seconds").Changed {
		o.TimeoutSeconds = nil
	}
	if !cmd.Flags().Lookup("period-seconds").Changed {
		o.PeriodSeconds = nil
	}
	if !cmd.Flags().Lookup("success-threshold").Changed {
		o.SuccessThreshold = nil
	}
	if !cmd.Flags().Lookup("failure-threshold").Changed {
		o.FailureThreshold = nil
	}

	if len(o.HTTPGet) > 0 {
		url, err := url.Parse(o.HTTPGet)
		if err != nil {
			return fmt.Errorf("--get-url could not be parsed as a valid URL: %v", err)
		}
		var host, port string
		if strings.Contains(url.Host, ":") {
			if host, port, err = net.SplitHostPort(url.Host); err != nil {
				return fmt.Errorf("--get-url did not have a valid port specification: %v", err)
			}
		}
		if host == "localhost" {
			host = ""
		}
		o.HTTPGetAction = &kapi.HTTPGetAction{
			Scheme: kapi.URIScheme(strings.ToUpper(url.Scheme)),
			Host:   host,
			Port:   intOrString(port),
			Path:   url.Path,
		}
	}

	return nil
}
func writeProbe(m map[string]interface{}, item *api.Probe) {
	if x, ok := m["initial_delay"].(int); ok {
		item.InitialDelaySeconds = x
	}
	if x, ok := m["timeout"].(int); ok {
		item.TimeoutSeconds = x
	}
	if x, ok := m["period"].(int); ok {
		item.PeriodSeconds = x
	}
	if x, ok := m["success_threshold"].(int); ok {
		item.SuccessThreshold = x
	}
	if x, ok := m["failure_threshold"].(int); ok {
		item.FailureThreshold = x
	}

	if n, ok := extractSingleMap(m["exec"]); ok {
		item.Exec = &api.ExecAction{}
		if l, ok := n["command"].([]interface{}); ok {
			for _, x := range l {
				item.Exec.Command = append(item.Exec.Command, x.(string))
			}
		}
	}

	if n, ok := extractSingleMap(m["http_get"]); ok {
		item.HTTPGet = &api.HTTPGetAction{}
		if x, ok := n["path"].(string); ok {
			item.HTTPGet.Path = x
		}
		if x, ok := n["port"].(int); ok {
			item.HTTPGet.Port = intstr.FromInt(x)
		}
		if x, ok := n["host"].(string); ok {
			item.HTTPGet.Host = x
		}
		if x, ok := n["scheme"].(string); ok {
			item.HTTPGet.Scheme = api.URIScheme(x)
		}
		if l, ok := n["http_header"].([]interface{}); ok {
			for _, x := range l {
				if y, ok := extractSingleMap(x); ok {
					h := api.HTTPHeader{}
					if x, ok := y["name"].(string); ok {
						h.Name = x
					}
					if x, ok := y["value"].(string); ok {
						h.Value = x
					}
					item.HTTPGet.HTTPHeaders = append(item.HTTPGet.HTTPHeaders, h)
				}
			}
		}
	}

	if n, ok := extractSingleMap(m["tcp_socket"]); ok {
		item.TCPSocket = &api.TCPSocketAction{}
		if x, ok := n["port"].(int); ok {
			item.TCPSocket.Port = intstr.FromInt(x)
		}
	}
}