Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to load yaml file #170

Closed
zysimplelife opened this issue Jan 19, 2018 · 6 comments
Closed

how to load yaml file #170

zysimplelife opened this issue Jan 19, 2018 · 6 comments

Comments

@zysimplelife
Copy link

I want to use this client to load yaml file for deployment or pod? does it support in this library?
Thanks

@karthikkondapally
Copy link
Contributor

karthikkondapally commented Jan 19, 2018

may be using snakeyaml to load yaml file and convert to object

Yaml yaml = new Yaml();
ExtensionsV1beta1Deployment body = yaml.loadAs(new FileReader("deployment.yaml"), ExtensionsV1beta1Deployment.class);
ExtensionsV1beta1Api api = new ExtensionsV1beta1Api();
api.createNamespacedDeployment("default", body, "");

@pycaster
Copy link

This is how I do it,

import io.kubernetes.client.ApiClient
import io.kubernetes.client.apis.CoreV1Api
import io.kubernetes.client.util.Config

  def setKubeApiClient() throws IOException, ApiException {
    StringReader reader = new StringReader(this.kubeConfig)
    ApiClient apiClient = Config.fromConfig(reader)
    Configuration.setDefaultApiClient(apiClient)
    this.api = new CoreV1Api()
  }

Where kubeConfig is a yaml.

@zysimplelife
Copy link
Author

zysimplelife commented Jan 22, 2018

@pycaster thanks for your answer, but it is only for create configuration. What I want to do is using yaml file to deploy resources, for example deployment or pod. I have tried @kondapally1989's way, it works. But the only problem is I have to know what kind of resource I want to create. For example, I have give the ExtensionsV1beta1Deployment.class explicitly when loading deployment.yaml. It is not that convenient.
Maybe in the future, this library can support automatically detection of resource type.
Please close this issue since we have kind of solution now. Thanks a lot.

@karthikkondapally
Copy link
Contributor

karthikkondapally commented Jan 22, 2018

@zysimplelife
I think we can improve upon previous solution itself by having predefined hasmap with mapping between kinds and and there class and a function convertYamlToobject().

i have written a rough and messy example class

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import io.kubernetes.client.ApiException;
import io.kubernetes.client.models.ExtensionsV1beta1Deployment;
import io.kubernetes.client.models.V1Namespace;
import io.kubernetes.client.models.V1Service;

public class Solution {

	private static final String yaml_file = "/home/karthik/Desktop/cloud/java/kubernetes/1.yaml";
	static Map<String,Object> objMap = new HashMap<>();
	static Yaml yaml = new Yaml();
	static {
		objMap.put("Deployment", ExtensionsV1beta1Deployment.class);
		objMap.put("Namespace",V1Namespace.class);
		objMap.put("Service", V1Service.class);
                //fill the hashmap
	}
	
	public static void main(String[] args) throws FileNotFoundException, ApiException {
		FileReader fr =new FileReader(yaml_file);
	    InputStream input = new FileInputStream(new File(yaml_file));
		Map map = (Map) yaml.load(input);
		ExtensionsV1beta1Deployment body = (ExtensionsV1beta1Deployment) convertyamlToObject(fr, (String) map.get("kind"));
		ExtensionsV1beta1Api api = new ExtensionsV1beta1Api();
		System.out.println(body);
	}
	
	public static Object convertyamlToObject(FileReader fr, String kind) {
		return yaml.loadAs(fr, (Class<Object>) objMap.get(kind));
	}
}

@zysimplelife
Copy link
Author

@kondapally1989
Yes, you are right, and it is exactly how Febric8 solve this problem.

Thanks
Charlie

@brendandburns
Copy link
Contributor

Fix for this is here:

#207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants