// SetCredential sets a credential by name.
func (scp *SecretsCredentialProvider) SetCredential(name string, credential *Credential) error {
	// Marshal the credential & base64 encode it.
	b, err := yaml.Marshal(credential)
	if err != nil {
		log.Printf("yaml marshal failed for credential named %s: %s", name, err)
		return err
	}

	enc := base64.StdEncoding.EncodeToString(b)

	// Then create a kubernetes object out of it
	metadata := make(map[string]string)
	metadata["name"] = name
	data := make(map[string]string)
	data["credential"] = enc
	obj := &util.KubernetesSecret{
		Kind:       secretType,
		APIVersion: "v1",
		Metadata:   metadata,
		Data:       data,
	}

	ko, err := yaml.Marshal(obj)
	if err != nil {
		log.Printf("yaml marshal failed for kubernetes object named %s: %s", name, err)
		return err
	}

	_, err = scp.k.Create(string(ko))
	return err
}
Example #2
0
// RunController creates a new replication controller named 'name' which creates 'replicas' pods running 'image'.
func RunController(ctx api.Context, image, name string, replicas int, client client.Interface, portSpec string, servicePort int) error {
	// TODO replace ctx with a namespace string
	if servicePort > 0 && !util.IsDNSLabel(name) {
		return fmt.Errorf("service creation requested, but an invalid name for a service was provided (%s). Service names must be valid DNS labels.", name)
	}
	ports, err := portsFromString(portSpec)
	if err != nil {
		return err
	}
	controller := &api.ReplicationController{
		ObjectMeta: api.ObjectMeta{
			Name: name,
		},
		Spec: api.ReplicationControllerSpec{
			Replicas: replicas,
			Selector: map[string]string{
				"name": name,
			},
			Template: &api.PodTemplateSpec{
				ObjectMeta: api.ObjectMeta{
					Labels: map[string]string{
						"name": name,
					},
				},
				Spec: api.PodSpec{
					Containers: []api.Container{
						{
							Name:  strings.ToLower(name),
							Image: image,
							Ports: ports,
						},
					},
				},
			},
		},
	}

	controllerOut, err := client.ReplicationControllers(api.Namespace(ctx)).Create(controller)
	if err != nil {
		return err
	}
	data, err := yaml.Marshal(controllerOut)
	if err != nil {
		return err
	}
	fmt.Print(string(data))

	if servicePort > 0 {
		svc, err := createService(ctx, name, servicePort, client)
		if err != nil {
			return err
		}
		data, err = yaml.Marshal(svc)
		if err != nil {
			return err
		}
		fmt.Printf(string(data))
	}
	return nil
}
Example #3
0
func (c *Config) Write() error {
	b, err := yaml.Marshal(c)
	if err != nil {
		return err
	}

	err = ioutil.WriteFile(saferc(), b, 0600)
	if err != nil {
		return err
	}

	url, token, skipverify, err := c.credentials()
	if err != nil {
		return err
	}

	b, err = yaml.Marshal(
		struct {
			URL        string `json:"vault"`
			Token      string `json:"token"`
			SkipVerify bool   `json:"skip_verify"`
		}{url, token, skipverify})
	if err != nil {
		return err
	}

	return ioutil.WriteFile(svtoken(), b, 0600)
}
Example #4
0
func TestParseInto(t *testing.T) {
	got := map[string]interface{}{
		"outer": map[string]interface{}{
			"inner1": "overwrite",
			"inner2": "value2",
		},
	}
	input := "outer.inner1=value1,outer.inner3=value3"
	expect := map[string]interface{}{
		"outer": map[string]interface{}{
			"inner1": "value1",
			"inner2": "value2",
			"inner3": "value3",
		},
	}

	if err := ParseInto(input, got); err != nil {
		t.Fatal(err)
	}

	y1, err := yaml.Marshal(expect)
	if err != nil {
		t.Fatal(err)
	}
	y2, err := yaml.Marshal(got)
	if err != nil {
		t.Fatalf("Error serializing parsed value: %s", err)
	}

	if string(y1) != string(y2) {
		t.Errorf("%s: Expected:\n%s\nGot:\n%s", input, y1, y2)
	}
}
func (scp *SecretsCredentialProvider) SetCredential(name string, credential *common.RegistryCredential) error {
	// Marshal the credential & base64 encode it.
	b, err := yaml.Marshal(credential)
	if err != nil {
		log.Printf("yaml marshal failed for credential: %s: %v", name, err)
		return err
	}
	enc := base64.StdEncoding.EncodeToString(b)

	// Then create a kubernetes object out of it
	metadata := make(map[string]string)
	metadata["name"] = name
	data := make(map[string]string)
	data["credential"] = enc
	obj := &common.KubernetesSecret{
		Kind:       secretType,
		ApiVersion: "v1",
		Metadata:   metadata,
		Data:       data,
	}
	ko, err := yaml.Marshal(obj)
	if err != nil {
		log.Printf("yaml marshal failed for kubernetes object: %s: %v", name, err)
		return err
	}
	log.Printf("Calling with: %s", string(ko))
	o, err := scp.k.Create(string(ko))
	log.Printf("Create returned: %s", o)
	return err
}
Example #6
0
// SaveChartfile saves the given metadata as a Chart.yaml file at the given path.
//
// 'filename' should be the complete path and filename ('foo/Chart.yaml')
func SaveChartfile(filename string, cf *chart.Metadata) error {
	out, err := yaml.Marshal(cf)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(filename, out, 0755)
}
Example #7
0
func (d *deployer) callServiceWithConfiguration(method, operation string, configuration *common.Configuration) (*common.Configuration, error) {
	callback := func(e error) error {
		return fmt.Errorf("cannot %s configuration: %s", operation, e)
	}

	y, err := yaml.Marshal(configuration)
	if err != nil {
		return nil, callback(err)
	}

	reader := ioutil.NopCloser(bytes.NewReader(y))
	resp, err := d.callService(method, d.getBaseURL(), reader, callback)

	if err != nil {
		return nil, err
	}

	result := &common.Configuration{}
	if len(resp) != 0 {
		if err := yaml.Unmarshal(resp, &result); err != nil {
			return nil, fmt.Errorf("cannot unmarshal response: (%v)", err)
		}
	}
	return result, nil
}
Example #8
0
File: local.go Project: slack/helm
// Reindex adds an entry to the index file at the given path
func Reindex(ch *chart.Chart, path string) error {
	name := ch.Metadata.Name + "-" + ch.Metadata.Version
	y, err := LoadIndexFile(path)
	if err != nil {
		return err
	}
	found := false
	for k := range y.Entries {
		if k == name {
			found = true
			break
		}
	}
	if !found {
		dig, err := provenance.DigestFile(path)
		if err != nil {
			return err
		}

		y.Add(ch.Metadata, name+".tgz", "http://localhost:8879/charts", "sha256:"+dig)

		out, err := yaml.Marshal(y)
		if err != nil {
			return err
		}

		ioutil.WriteFile(path, out, 0644)
	}
	return nil
}
func DoParseTest(t *testing.T, storage string, obj runtime.Object, codec runtime.Codec, p *Parser) {
	jsonData, _ := codec.Encode(obj)
	var tmp map[string]interface{}
	json.Unmarshal(jsonData, &tmp)
	yamlData, _ := yaml.Marshal(tmp)
	t.Logf("Intermediate yaml:\n%v\n", string(yamlData))
	t.Logf("Intermediate json:\n%v\n", string(jsonData))
	jsonGot, jsonErr := p.ToWireFormat(jsonData, storage, latest.Codec, codec)
	yamlGot, yamlErr := p.ToWireFormat(yamlData, storage, latest.Codec, codec)

	if jsonErr != nil {
		t.Errorf("json err: %#v", jsonErr)
	}
	if yamlErr != nil {
		t.Errorf("yaml err: %#v", yamlErr)
	}
	if string(jsonGot) != string(jsonData) {
		t.Errorf("json output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
			string(jsonGot), string(jsonData))
	}
	if string(yamlGot) != string(jsonData) {
		t.Errorf("yaml parsed output didn't match:\nGot:\n%v\n\nWanted:\n%v\n",
			string(yamlGot), string(jsonData))
	}
}
Example #10
0
// writeConfig
func writeConfig(path string, v interface{}) error {

	// take a config objects path and create (and truncate) the file, preparing it
	// to receive new configurations
	f, err := os.Create(path)
	if err != nil {
		Fatal("[config/config] os.Create() failed", err.Error())
	}
	defer f.Close()

	// marshal the config object
	b, err := yaml.Marshal(v)
	if err != nil {
		Fatal("[config/config] yaml.Marshal() failed", err.Error())
	}

	// mutex.Lock()

	// write it back to the file
	if _, err := f.Write(b); err != nil {
		return err
	}

	// mutex.Unlock()

	return nil
}
Example #11
0
func TestValidateSchema(t *testing.T) {

	Convey("Test valid schema", t, func() {
		config := testConfig{"Tom", "Justin"}
		cfgValidator = &schemaValidatorType{}
		jb, _ := json.Marshal(config)
		errs := ValidateSchema(MOCK_CONSTRAINTS, string(jb))
		So(errs, ShouldBeNil)
	})

	Convey("Test invalid schema", t, func() {
		config := testConfig{"Tom", "Justin"}
		cfgValidator = &schemaValidatorType{}
		jb, _ := json.Marshal(config)
		errs := ValidateSchema(INVALID_MOCK_CONSTRAINTS, string(jb))
		So(errs, ShouldNotBeNil)
	})

	Convey("Test invalid json", t, func() {
		config := testConfig{"Tom", "Justin"}
		cfgValidator = &schemaValidatorType{}
		yb, _ := yaml.Marshal(config)
		errs := ValidateSchema(MOCK_CONSTRAINTS, string(yb))
		So(errs, ShouldNotBeNil)
	})
}
// getDependencies iterates over resources and returns a map of resource name to
// the set of dependencies that resource has.
//
// Dependencies are reversed for delete operation.
func getDependencies(c *common.Configuration, o operation) (DependencyMap, error) {
	deps := DependencyMap{}

	// Prepopulate map. This will be used later to validate referenced resources
	// actually exist.
	for _, r := range c.Resources {
		deps[r.Name] = make(map[string]bool)
	}

	for _, r := range c.Resources {
		props, err := yaml.Marshal(r.Properties)
		if err != nil {
			return nil, fmt.Errorf("Failed to deserialize resource properties for resource %s: %v", r.Name, r.Properties)
		}

		refs := refRe.FindAllStringSubmatch(string(props), -1)
		for _, ref := range refs {
			// Validate referenced resource exists in config.
			if _, ok := deps[ref[1]]; !ok {
				return nil, fmt.Errorf("Invalid resource name in reference: %s", ref[1])
			}

			// Delete dependencies should be reverse of create.
			if o == DeleteOperation {
				deps[ref[1]][r.Name] = true
			} else {
				deps[r.Name][ref[1]] = true
			}
		}
	}

	return deps, nil
}
func toYAML(v interface{}) string {
	y, err := yaml.Marshal(v)
	if err != nil {
		panic(fmt.Sprintf("yaml marshal failed: %v", err))
	}
	return string(y)
}
Example #14
0
// WriteFile writes a repositories file to the given path.
func (r *RepoFile) WriteFile(path string, perm os.FileMode) error {
	data, err := yaml.Marshal(r)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(path, data, perm)
}
Example #15
0
func generateReplicationController(name string, image string, replicas int, ports []v1beta1.Port) {
	controller := []v1beta1.ReplicationController{{
		TypeMeta: v1beta1.TypeMeta{APIVersion: "v1beta1", Kind: "ReplicationController", ID: name},
		DesiredState: v1beta1.ReplicationControllerState{
			Replicas: replicas,
			ReplicaSelector: map[string]string{
				"simpleservice": name,
			},
			PodTemplate: v1beta1.PodTemplate{
				DesiredState: v1beta1.PodState{
					Manifest: v1beta1.ContainerManifest{
						Version: "v1beta2",
						Containers: []v1beta1.Container{
							{
								Name:  name,
								Image: image,
								Ports: ports,
							},
						},
					},
				},
				Labels: map[string]string{
					"simpleservice": name,
				},
			},
		},
		Labels: map[string]string{
			"simpleservice": name,
		},
	}}
	controllerOutData, err := yaml.Marshal(controller)
	checkErr(err)

	fmt.Print(string(controllerOutData))
}
Example #16
0
File: index.go Project: slack/helm
// WriteFile writes an index file to the given destination path.
//
// The mode on the file is set to 'mode'.
func (i IndexFile) WriteFile(dest string, mode os.FileMode) error {
	b, err := yaml.Marshal(i)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(dest, b, mode)
}
Example #17
0
// YAML converts a Secret to its YAML representation and returns it as a string.
// Returns an empty string if there were any errors.
func (s *Secret) YAML() string {
	b, err := yaml.Marshal(s.data)
	if err != nil {
		return ""
	}
	return string(b)
}
Example #18
0
func main() {
	if len(os.Args) != 3 {
		checkErr(fmt.Errorf(usage))
	}
	specFilename := os.Args[1]
	configFilename := os.Args[2]

	specData, err := ReadConfigData(specFilename)
	checkErr(err)

	spec := EnscopeSpec{}
	err = yaml.Unmarshal(specData, &spec)
	checkErr(err)

	configData, err := ReadConfigData(configFilename)
	checkErr(err)

	var data interface{}

	err = yaml.Unmarshal([]byte(configData), &data)
	checkErr(err)

	xData, err := enscope("", spec, data)
	checkErr(err)

	out, err := yaml.Marshal(xData)
	checkErr(err)

	fmt.Print(string(out))
}
Example #19
0
// CopyCharts takes a glob expression and copies those charts to the server root.
func (s *Server) CopyCharts(origin string) ([]string, error) {
	files, err := filepath.Glob(origin)
	if err != nil {
		return []string{}, err
	}
	copied := make([]string, len(files))
	for i, f := range files {
		base := filepath.Base(f)
		newname := filepath.Join(s.docroot, base)
		data, err := ioutil.ReadFile(f)
		if err != nil {
			return []string{}, err
		}
		if err := ioutil.WriteFile(newname, data, 0755); err != nil {
			return []string{}, err
		}
		copied[i] = newname
	}

	// generate the index
	index, err := repo.IndexDirectory(s.docroot, s.URL())
	if err != nil {
		return copied, err
	}

	d, err := yaml.Marshal(index.Entries)
	if err != nil {
		return copied, err
	}

	ifile := filepath.Join(s.docroot, "index.yaml")
	err = ioutil.WriteFile(ifile, d, 0755)
	return copied, err
}
Example #20
0
func (r *ChartRepository) saveIndexFile() error {
	index, err := yaml.Marshal(r.IndexFile)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(filepath.Join(r.RootPath, indexPath), index, 0644)
}
Example #21
0
func generateService(name string, portSpec string) {
	if portSpec == "" {
		return
	}

	servicePort, containerPort, err := portsFromString(portSpec)
	checkErr(err)

	svc := []v1beta3.Service{{
		TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "Service"},
		ObjectMeta: v1beta3.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"service": name,
			},
		},
		Spec: v1beta3.ServiceSpec{
			Port:          servicePort,
			ContainerPort: util.NewIntOrStringFromInt(containerPort),
			Selector: map[string]string{
				"service": name,
			},
		},
	}}

	svcOutData, err := yaml.Marshal(svc)
	checkErr(err)

	fmt.Print(string(svcOutData))
}
func (a *Configurator) configureResource(resource *common.Resource, o operation) (string, error) {
	args := []string{o.String()}
	if o == GetOperation {
		args = append(args, "-o", "yaml")
		if resource.Type != "" {
			args = append(args, resource.Type)
			if resource.Name != "" {
				args = append(args, resource.Name)
			}
		}
	}

	var y []byte
	if len(resource.Properties) > 0 {
		var err error
		y, err = yaml.Marshal(resource.Properties)
		if err != nil {
			e := fmt.Errorf("yaml marshal failed for resource: %v: %v", resource.Name, err)
			resource.State = failState(e)
			return "", e
		}
	}

	if len(y) > 0 {
		args = append(args, "-f", "-")
	}

	args = append(args, a.Arguments...)
	cmd := exec.Command(a.KubePath, args...)
	cmd.Stdin = bytes.NewBuffer(y)

	// Combine stdout and stderr into a single dynamically resized buffer
	combined := &bytes.Buffer{}
	cmd.Stdout = combined
	cmd.Stderr = combined

	if err := cmd.Start(); err != nil {
		e := fmt.Errorf("cannot start kubetcl for resource: %v: %v", resource.Name, err)
		resource.State = failState(e)
		return "", e
	}

	if err := cmd.Wait(); err != nil {
		// Treat delete special. If a delete is issued and a resource is not found, treat it as
		// success.
		if o == DeleteOperation && strings.HasSuffix(strings.TrimSpace(combined.String()), "not found") {
			log.Println(resource.Name + " not found, treating as success for delete")
		} else {
			e := fmt.Errorf("kubetcl failed for resource: %v: %v: %v", resource.Name, err, combined.String())
			resource.State = failState(e)
			return combined.String(), e
		}
	}

	log.Printf("kubectl succeeded for resource: %v: SysTime: %v UserTime: %v\n%v",
		resource.Name, cmd.ProcessState.SystemTime(), cmd.ProcessState.UserTime(), combined.String())

	resource.State = &common.ResourceState{Status: common.Created}
	return combined.String(), nil
}
Example #23
0
func generatePodTemplate(sname string, cname string, podSpec v1beta3.PodSpec) {
	name := fmt.Sprintf("%s-%s", sname, cname)
	pt := []v1beta3.PodTemplate{{
		TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "PodTemplate"},
		ObjectMeta: v1beta3.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"service": sname,
				"track":   cname,
			},
		},
		Spec: v1beta3.PodTemplateSpec{
			ObjectMeta: v1beta3.ObjectMeta{
				Labels: map[string]string{
					"service": sname,
					"track":   cname,
				},
			},
			Spec: podSpec,
		},
	}}

	ptOutData, err := yaml.Marshal(pt)
	checkErr(err)

	fmt.Print(string(ptOutData))
}
Example #24
0
func (y YamlFormatter) Output(ui cli.Ui, secret *api.Secret, data interface{}) error {
	b, err := yaml.Marshal(data)
	if err == nil {
		ui.Output(strings.TrimSpace(string(b)))
	}
	return err
}
Example #25
0
func generateReplicationController(sname string, cname string, replicas int) {
	if replicas < 1 {
		replicas = 1
	}

	name := fmt.Sprintf("%s-%s", sname, cname)
	rc := []v1beta3.ReplicationController{{
		TypeMeta: v1beta3.TypeMeta{APIVersion: "v1beta3", Kind: "ReplicationController"},
		ObjectMeta: v1beta3.ObjectMeta{
			Name: name,
			Labels: map[string]string{
				"service": sname,
				"track":   cname,
			},
		},
		Spec: v1beta3.ReplicationControllerSpec{
			Replicas: replicas,
			Selector: map[string]string{
				"service": sname,
				"track":   cname,
			},
			Template: v1beta3.ObjectReference{
				Kind:       "PodTemplate",
				Name:       name,
				APIVersion: "v1beta3",
			},
		},
	}}

	rcOutData, err := yaml.Marshal(rc)
	checkErr(err)

	fmt.Print(string(rcOutData))
}
Example #26
0
func TestConfigFileOtherFields(t *testing.T) {
	yc := struct {
		ProxyCfgFile string `json:"proxy"`
	}{
		"readonly",
	}

	b, err := yaml.Marshal(&yc)
	if err != nil {
		t.Fatal(err)
	}

	tmpfile := mustCreateCfgFile(t, b)
	defer os.Remove(tmpfile.Name())

	args := []string{
		fmt.Sprintf("--config-file=%s", tmpfile.Name()),
	}

	cfg := newConfig()
	err = cfg.parse(args)
	if err != nil {
		t.Fatal(err)
	}

	validateOtherFlags(t, cfg)
}
// Marshal creates and returns an ExpansionResponse from an ExpansionResult.
func (eResult *ExpansionResult) Marshal() (*ExpansionResponse, error) {
	configYaml, err := yaml.Marshal(eResult.Config)
	if err != nil {
		return nil, fmt.Errorf("cannot marshal manifest template (%s):\n%s", err, eResult.Config)
	}

	layoutYaml, err := yaml.Marshal(eResult.Layout)
	if err != nil {
		return nil, fmt.Errorf("cannot marshal manifest layout (%s):\n%s", err, eResult.Layout)
	}

	return &ExpansionResponse{
		Config: string(configYaml),
		Layout: string(layoutYaml),
	}, nil
}
Example #28
0
func TestDecodeSinglePod(t *testing.T) {
	pod := &api.Pod{
		TypeMeta: api.TypeMeta{
			APIVersion: "",
		},
		ObjectMeta: api.ObjectMeta{
			Name:      "test",
			UID:       "12345",
			Namespace: "mynamespace",
		},
		Spec: api.PodSpec{
			RestartPolicy: api.RestartPolicyAlways,
			DNSPolicy:     api.DNSClusterFirst,
			Containers: []api.Container{{
				Name:                   "image",
				Image:                  "test/image",
				ImagePullPolicy:        "IfNotPresent",
				TerminationMessagePath: "/dev/termination-log",
				SecurityContext:        securitycontext.ValidSecurityContextWithContainerDefaults(),
			}},
		},
	}
	json, err := testapi.Codec().Encode(pod)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}
	parsed, podOut, err := tryDecodeSinglePod(json, noDefault)
	if !parsed {
		t.Errorf("expected to have parsed file: (%s)", string(json))
	}
	if err != nil {
		t.Errorf("unexpected error: %v (%s)", err, string(json))
	}
	if !reflect.DeepEqual(pod, podOut) {
		t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(json))
	}

	for _, version := range registered.RegisteredVersions {
		externalPod, err := testapi.Converter().ConvertToVersion(pod, version)
		if err != nil {
			t.Errorf("unexpected error: %v", err)
		}
		yaml, err := yaml.Marshal(externalPod)
		if err != nil {
			t.Errorf("unexpected error: %v", err)
		}

		parsed, podOut, err = tryDecodeSinglePod(yaml, noDefault)
		if !parsed {
			t.Errorf("expected to have parsed file: (%s)", string(yaml))
		}
		if err != nil {
			t.Errorf("unexpected error: %v (%s)", err, string(yaml))
		}
		if !reflect.DeepEqual(pod, podOut) {
			t.Errorf("expected:\n%#v\ngot:\n%#v\n%s", pod, podOut, string(yaml))
		}
	}
}
Example #29
0
// PrintObj prints the data as YAML.
func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
	output, err := yaml.Marshal(obj)
	if err != nil {
		return err
	}
	_, err = fmt.Fprint(w, string(output))
	return err
}
Example #30
0
func toYaml(v interface{}) string {
	data, err := yaml.Marshal(v)
	if err != nil {
		// Swallow errors inside of a template.
		return ""
	}
	return string(data)
}