目前我有一段基于其他示例的go代码。我可以列出所有的豆荚,工作......等等,但是我在 Openshift 上创建了一个相当棘手的问题。
我的代码的以下部分应该创建一个Job,我甚至得到一个响应,但是没有在提到的命名空间上创建任务:
func main() {
var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
panic(err)
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err)
}
jobsClient := clientset.BatchV1().Jobs("gitlab")
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "demo-job",
},
Spec: batchv1.JobSpec{
Template: apiv1.PodTemplateSpec{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "demo",
Image: "myimage",
},
},
},
},
},
}
fmt.Println("Creating job... ")
result1, err1 := jobsClient.Create(job)
if err != nil {
fmt.Println(err1)
panic(err1)
}
fmt.Printf("Created job %q.\n", result1)
}
结果,我得到的就是:
Created job "&Job{ObjectMeta:k8s_io_apimachinery_pkg_apis_meta_v1.ObjectMeta{Name:,GenerateName:,Namespace:,SelfLink:,UID:,ResourceVersion:,Generation:0,CreationTimestamp:0001-01-01 00:00:00 +0000 UTC,DeletionTimestamp:<nil>,DeletionGracePeriodSeconds:nil,Labels:map[string]string{},Annotations:map[string]string{},OwnerReferences:[],Finalizers:[],ClusterName:,Initializers:nil,},Spec:JobSpec{Parallelism:nil,Completions:nil,ActiveDeadlineSeconds:nil,Selector:nil,ManualSelector:nil,Template:k8s_io_api_core_v1.PodTemplateSpec{ObjectMeta:k8s_io_apimachinery_pkg_apis_meta_v1.ObjectMeta{Name:,GenerateName:,Namespace:,SelfLink:,UID:,ResourceVersion:,Generation:0,CreationTimestamp:0001-01-01 00:00:00 +0000 UTC,DeletionTimestamp:<nil>,DeletionGracePeriodSeconds:nil,Labels:map[string]string{},Annotations:map[string]string{},OwnerReferences:[],Finalizers:[],ClusterName:,Initializers:nil,},Spec:PodSpec{Volumes:[],Containers:[],RestartPolicy:,TerminationGracePeriodSeconds:nil,ActiveDeadlineSeconds:nil,DNSPolicy:,NodeSelector:map[string]string{},ServiceAccountName:,DeprecatedServiceAccount:,NodeName:,HostNetwork:false,HostPID:false,HostIPC:false,SecurityContext:nil,ImagePullSecrets:[],Hostname:,Subdomain:,Affinity:nil,SchedulerName:,InitContainers:[],AutomountServiceAccountToken:nil,Tolerations:[],HostAliases:[],PriorityClassName:,Priority:nil,DNSConfig:nil,},},BackoffLimit:nil,},Status:JobStatus{Conditions:[],StartTime:<nil>,CompletionTime:<nil>,Active:0,Succeeded:0,Failed:0,},}".
检查&#34; gitlab&#34;命名空间,没有新工作。
答案 0 :(得分:3)
根据我过去所做的工作(你可以看到here),我认为你可以在Job资源的ObjectMeta中指定命名空间。你可以把它放在jobsClient中。
jobsClient := clientset.BatchV1().Jobs()
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
Name: "demo-job",
Namespace: "gitlab",
},
Spec: batchv1.JobSpec{
Template: apiv1.PodTemplateSpec{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "demo",
Image: "myimage",
},
},
},
},
},
}
答案 1 :(得分:0)
想出来,即使你没有得到任何错误,你必须指定所有必需的字段,所以取得工作结果,看看你是否可以手动创建对象。
话虽如此,之前的Job需要按照以下方式创建:
job := &batchv1.Job{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "prometheus-webhook-",
Namespace: "gitlab",
},
Spec: batchv1.JobSpec{
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "prometheus-webhook-",
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "ansible-job",
Image: "yourimage",
},
},
RestartPolicy: apiv1.RestartPolicyOnFailure,
},
},
},
}