prow/cmd/mkbuild-cluster/main.go
func do(o options) error {
	// Refresh credentials if requested
	if o.getClientCert {
		if err := getCredentials(o); err != nil {
			return fmt.Errorf("get client cert: %v", err)
		}
	}
	// Create the new cluster entry
	d, err := describeCluster(o)
	if err != nil {
		return fmt.Errorf("describe auth: %v", err)
	}
	newCluster := kube.Cluster{
		Endpoint:             "https://" + d.Endpoint,
		ClusterCACertificate: d.Auth.ClusterCACertificate,
		ClientKey:            d.Auth.ClientKey,
		ClientCertificate:    d.Auth.ClientCertificate,
	}

	// Try to use this entry
	if !o.skipCheck {
		c, err := kube.NewClient(&newCluster, "kube-system")
		if err != nil {
			return fmt.Errorf("create client: %v", err)
		}
		if _, err = c.ListPods("k8s-app=kube-dns"); err != nil {
			logrus.WithError(err).Errorf("Failed to validate credentials (consider --get-client-cert)")
			return fmt.Errorf("list all pods to check new credentials: %v", err)
		}
	}

	// Just print this entry if requested
	if o.printEntry {
		data, err := kube.MarshalClusterMap(map[string]kube.Cluster{o.alias: newCluster})
		if err != nil {
			return fmt.Errorf("marshal %s: %v", o.alias, err)
		}
		fmt.Println(string(data))
		return nil
	}

	// Append the new entry to the current secret

	// First read in the secret from stdin
	b, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return fmt.Errorf("read stdin: %v", err)
	}
	var s coreapi.Secret
	if err := yaml.Unmarshal(b, &s); err != nil {
		return fmt.Errorf("unmarshal stdin: %v", err)
	}

	// Now decode the {alias: cluster} map and print out current keys
	clusters, err := kube.UnmarshalClusterMap(s.Data["cluster"])
	if err != nil {
		return fmt.Errorf("unmarshal secret: %v", err)
	}
	var existing []string
	for a := range clusters {
		existing = append(existing, a)
	}
	logrus.Infof("Existing clusters: %s", strings.Join(existing, ", "))

	// Add new key
	_, ok := clusters[o.alias]
	if ok && !o.overwrite {
		return fmt.Errorf("cluster %s already exists", o.alias)
	}
	clusters[o.alias] = newCluster
	logrus.Infof("New cluster: %s", o.alias)

	// Marshal the {alias: cluster} map back into secret data
	data, err := kube.MarshalClusterMap(clusters)
	if err != nil {
		return fmt.Errorf("marshal clusters: %v", err)
	}

	if o.printData { // Just print the data outside of the secret
		fmt.Println(string(data))
		return nil
	}

	// Output the new secret
	s.Data["cluster"] = data
	buf, err := yaml.Marshal(s)
	if err != nil {
		return fmt.Errorf("marshal secret: %v", err)
	}
	fmt.Println(string(buf))
	return nil
}
func do(o options) error {
	// Refresh credentials if requested
	if o.getClientCert {
		if err := getCredentials(o); err != nil {
			return fmt.Errorf("get client cert: %v", err)
		}
	}
	// Create the new cluster entry
	d, err := describeCluster(o)
	if err != nil {
		return fmt.Errorf("describe auth: %v", err)
	}
	newCluster := kube.Cluster{
		Endpoint:             "https://" + d.Endpoint,
		ClusterCACertificate: d.Auth.ClusterCACertificate,
		ClientKey:            d.Auth.ClientKey,
		ClientCertificate:    d.Auth.ClientCertificate,
	}

	// Try to use this entry
	if !o.skipCheck {
		c, err := kube.NewClient(&newCluster, "kube-system")
		if err != nil {
			return fmt.Errorf("create client: %v", err)
		}
		if _, err = c.ListPods("k8s-app=kube-dns"); err != nil {
			logrus.WithError(err).Errorf("Failed to validate credentials (consider --get-client-cert)")
			return fmt.Errorf("list all pods to check new credentials: %v", err)
		}
	}

	// Just print this entry if requested
	if o.printEntry {
		data, err := kube.MarshalClusterMap(map[string]kube.Cluster{o.alias: newCluster})
		if err != nil {
			return fmt.Errorf("marshal %s: %v", o.alias, err)
		}
		fmt.Println(string(data))
		return nil
	}

	// Append the new entry to the current secret

	// First read in the secret from stdin
	b, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		return fmt.Errorf("read stdin: %v", err)
	}
	var s coreapi.Secret
	if err := yaml.Unmarshal(b, &s); err != nil {
		return fmt.Errorf("unmarshal stdin: %v", err)
	}

	// Now decode the {alias: cluster} map and print out current keys
	clusters, err := kube.UnmarshalClusterMap(s.Data["cluster"])
	if err != nil {
		return fmt.Errorf("unmarshal secret: %v", err)
	}
	var existing []string
	for a := range clusters {
		existing = append(existing, a)
	}
	logrus.Infof("Existing clusters: %s", strings.Join(existing, ", "))

	// Add new key
	_, ok := clusters[o.alias]
	if ok && !o.overwrite {
		return fmt.Errorf("cluster %s already exists", o.alias)
	}
	clusters[o.alias] = newCluster
	logrus.Infof("New cluster: %s", o.alias)

	// Marshal the {alias: cluster} map back into secret data
	data, err := kube.MarshalClusterMap(clusters)
	if err != nil {
		return fmt.Errorf("marshal clusters: %v", err)
	}

	if o.printData { // Just print the data outside of the secret
		fmt.Println(string(data))
		return nil
	}

	// Output the new secret
	s.Data["cluster"] = data
	buf, err := yaml.Marshal(s)
	if err != nil {
		return fmt.Errorf("marshal secret: %v", err)
	}
	fmt.Println(string(buf))
	return nil
}
// do will get creds for the specified cluster and add them to the stdin secret