Using Lists or triple dashes to put multiple Kubernetes objects in one YAML file: purely a stylistic choice?
Date : March 29 2020, 07:55 AM
I wish this helpful for you I can think of two reasons: Because the Kubernetes API works with JSON and in JSON there is no --- Maybe the kind List is meant only for responses.
|
Using ruamel.yaml to update yaml block in yaml file which contains multiple yamls
Tag : python , By : Joe Sweeney
Date : March 29 2020, 07:55 AM
With these it helps Your file consists of multiple YAML documents, this is what you read in using round_trip_load_all, which gives you a generator. If you write that back using round_trip_dump(), you are never going to get the --- YAML document separators that are in the original file. import sys
import ruamel.yaml
yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
data = list(yaml.load_all(open('app.yaml')))
# parse the most recent git commit sha from command line
# docker_image = 'blockfreight/go-bftx:ci-cd-' + check_output('git log -1 -- pretty=format:%h'.split()).decode()
docker_image = 'blockfreight/go-bftx:ci-cd-' + 'check_output_output'
# update go-bftx image with most recent git-commit-sha tag in the StatefulSet block
data[-1]['spec']['template']['spec']['containers'][1]['image'] = docker_image
with open('out.yaml', 'w') as ofp:
yaml.dump_all(data, ofp)
import sys
import ruamel.yaml
with open('out.yaml', 'w') as ofp:
lines = ''
with open('app.yaml') as ifp:
for line in ifp:
lines += line
if line == '---\n':
ofp.write(lines)
lines = ''
# process lines from the last document
# print(lines)
yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True
data = yaml.load(lines)
# parse the most recent git commit sha from command line
# docker_image = 'blockfreight/go-bftx:ci-cd-' + check_output('git log -1 -- pretty=format:%h'.split()).decode()
docker_image = 'blockfreight/go-bftx:ci-cd-' + 'check_output_output'
# update go-bftx image with most recent git-commit-sha tag in the StatefulSet block
data['spec']['template']['spec']['containers'][1]['image'] = docker_image
yaml.dump(data, ofp)
--- app.yaml 2018-06-23 14:41:02.256290577 +0200
+++ out.yaml 2018-06-23 14:58:09.933991459 +0200
@@ -143,7 +143,7 @@
spec:
selector:
matchLabels:
- app: bftx
+ app: bftx
serviceName: blockfreight
replicas: 1
template:
@@ -151,7 +151,7 @@
labels:
app: bftx
spec:
- containers:
+ containers:
- name: tm
imagePullPolicy: IfNotPresent
image: tendermint/tendermint:0.20.0
@@ -199,7 +199,7 @@
tendermint node --moniker="`hostname`" --p2p.seeds="aeabbf6b891435013f2a800fa9e22a1451ca90fd@bftx0.blockfreight.net:8888,6e9515c2cfed19464e6ce11ba2297ecdb411103b@bftx1.blockfreight.net:8888,b8b988370783bd0e58bf926d621a47160af2bdae@bftx2.blockfreight.net:8888,8c091f4e3dc4ac27db1efd38beee012d99967fd8@bftx3.blockfreight.net:8888" --proxy_app="tcp://localhost:46658" --consensus.create_empty_blocks=false
- name: app
imagePullPolicy: Always
- image: blockfreight/go-bftx:rc1
+ image: blockfreight/go-bftx:ci-cd-check_output_output
ports:
- containerPort: 12345
- containerPort: 46658
@@ -247,7 +247,7 @@
- mountPath: /etc/nginx/conf.d/pub_key.conf
name: tmconfigdir
subPath: pub_key_nginx.conf
- volumes:
+ volumes:
- name: tmconfigdir
configMap:
name: bftx-config
@@ -262,7 +262,7 @@
annotations:
volume.alpha.kubernetes.io/storage-class: anything
spec:
- accessModes: [ "ReadWriteOnce" ]
+ accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 2Gi
@@ -271,7 +271,7 @@
annotations:
volume.alpha.kubernetes.io/storage-class: anything
spec:
- accessModes: [ "ReadWriteOnce" ]
+ accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 12Mi
|
How can i check downloaded images in kubernetes(I'm using image in kubernetes yaml file)? Or, what is similar to 'docker
Date : March 29 2020, 07:55 AM
Hope that helps There is no equivalent command. The kubelet downloads images on demand, but really it just passes those commands on to the underlying system so to check which images are actually resident, you would need to know what CRI plugin you are using and talk to that. In general you don’t worry much about images in Kubernetes, they are automatically managed by the kubelet for you.
|
How to handle multiple ports under the same container in kubernetes yaml file
Date : March 29 2020, 07:55 AM
This might help you snippet inside docker-compose.yml spec:
containers:
- name: graphite
image: sitespeedio/graphite:1.1.5-3
restartPolicy: Always
ports:
- containerPort: 2003
name: graphite_two
- containerPort: 8080
name: graphite_one
|
Using client-go to `kubectl apply` against the Kubernetes API directly with multiple types in a single YAML file
Date : March 29 2020, 07:55 AM
wish helps you It sounds like you've figured out how to deserialize YAML files into Kubernetes runtime.Objects, but the problem is dynamically deploying a runtime.Object without writing special code for each Kind. kubectl achieves this by interacting with the REST API directly. Specifically, via resource.Helper. import (
meta "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/restmapper"
"k8s.io/apimachinery/pkg/runtime"
)
func createObject(kubeClientset kubernetes.Interface, restConfig rest.Config, obj runtime.Object) error {
// Create a REST mapper that tracks information about the available resources in the cluster.
groupResources, err := restmapper.GetAPIGroupResources(kubeClientset.Discovery())
if err != nil {
return err
}
rm := restmapper.NewDiscoveryRESTMapper(groupResources)
// Get some metadata needed to make the REST request.
gvk := obj.GetObjectKind().GroupVersionKind()
gk := schema.GroupKind{Group: gvk.Group, Kind: gvk.Kind}
mapping, err := rm.RESTMapping(gk, gvk.Version)
if err != nil {
return err
}
name, err := meta.NewAccessor().Name(obj)
if err != nil {
return err
}
// Create a client specifically for creating the object.
restClient, err := newRestClient(restConfig, mapping.GroupVersionKind.GroupVersion())
if err != nil {
return err
}
// Use the REST helper to create the object in the "default" namespace.
restHelper := resource.NewHelper(restClient, mapping)
return restHelper.Create("default", false, obj, &metav1.CreateOptions{})
}
func newRestClient(restConfig rest.Config, gv schema.GroupVersion) (rest.Interface, error) {
restConfig.ContentConfig = resource.UnstructuredPlusDefaultContentConfig()
restConfig.GroupVersion = &gv
if len(gv.Group) == 0 {
restConfig.APIPath = "/api"
} else {
restConfig.APIPath = "/apis"
}
return rest.RESTClientFor(&restConfig)
}
|