1. Structure
1.1 ConfigMapList
Package: "/api/core/v1"
type ConfigMapList struct { `json:",inline"` `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` Items []ConfigMap `json:"items" protobuf:"bytes,2,rep,name=items"` }
Items
Each ConfigMap structure is as follows:
1.2 ConfigMap
Package: "/api/core/v1"
type ConfigMap struct { `json:",inline"` `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"` Immutable *bool `json:"immutable,omitempty" protobuf:"varint,4,opt,name=immutable"` Data map[string]string `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"` BinaryData map[string][]byte `json:"binaryData,omitempty" protobuf:"bytes,3,rep,name=binaryData"` }
Its members are described as follows:
1.3 TypeMeta
Package: "/apimachinery/pkg/apis/meta/v1"
type TypeMeta struct { Kind string `json:"kind,omitempty" protobuf:"bytes,1,opt,name=kind"` APIVersion string `json:"apiVersion,omitempty" protobuf:"bytes,2,opt,name=apiVersion"` }
The following sections corresponding to the yml file that creates the service on k8s:
apiVersion: v1 kind: ConfigMap
1.4 ObjectMeta
Package: "/apimachinery/pkg/apis/meta/v1"
type ObjectMeta struct { Name string `json:"name,omitempty" protobuf:"bytes,1,opt,name=name"` GenerateName string `json:"generateName,omitempty" protobuf:"bytes,2,opt,name=generateName"` Namespace string `json:"namespace,omitempty" protobuf:"bytes,3,opt,name=namespace"` SelfLink string `json:"selfLink,omitempty" protobuf:"bytes,4,opt,name=selfLink"` UID `json:"uid,omitempty" protobuf:"bytes,5,opt,name=uid,casttype=/kubernetes/pkg/"` ResourceVersion string `json:"resourceVersion,omitempty" protobuf:"bytes,6,opt,name=resourceVersion"` Generation int64 `json:"generation,omitempty" protobuf:"varint,7,opt,name=generation"` CreationTimestamp Time `json:"creationTimestamp,omitempty" protobuf:"bytes,8,opt,name=creationTimestamp"` DeletionTimestamp *Time `json:"deletionTimestamp,omitempty" protobuf:"bytes,9,opt,name=deletionTimestamp"` DeletionGracePeriodSeconds *int64 `json:"deletionGracePeriodSeconds,omitempty" protobuf:"varint,10,opt,name=deletionGracePeriodSeconds"` Labels map[string]string `json:"labels,omitempty" protobuf:"bytes,11,rep,name=labels"` Annotations map[string]string `json:"annotations,omitempty" protobuf:"bytes,12,rep,name=annotations"` OwnerReferences []OwnerReference `json:"ownerReferences,omitempty" patchStrategy:"merge" patchMergeKey:"uid" protobuf:"bytes,13,rep,name=ownerReferences"` Finalizers []string `json:"finalizers,omitempty" patchStrategy:"merge" protobuf:"bytes,14,rep,name=finalizers"` ManagedFields []ManagedFieldsEntry `json:"managedFields,omitempty" protobuf:"bytes,17,rep,name=managedFields"` }
The following sections corresponding to the yml file that creates the service on k8s:
metadata: name: nginxconf namespace: test
1.7 Example of comparison yml file
Attached a configMap information on the native k8s cluster, you can compare and understand the above structure
apiVersion: v1 data: : |2- worker_processes 1; events { worker_connections 1024; } http { include ; default_type application/octet-stream; client_max_body_size 50m; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root /usr/share/nginx/html; location / { index ; } } } kind: ConfigMap metadata: creationTimestamp: "2022-10-14T06:53:14Z" name: nginxconf namespace: liubei resourceVersion: "23364643" selfLink: /api/v1/namespaces/liubei/configmaps/nginxconf uid: cbe236fb-b86b-47f5-bf13-696fada4e400
1.5 Immutable
Pointer to bool value
- true, not changeable
- Otherwise, it can be changed at any time
1.6 Data
map[string]string
Type, corresponding to the yaml filedata
Field, each member corresponds to a key-value pair, that is, a configuration file to be mounted
1.7 BinaryData
map[string][]byte
Type, andData
Similar, except that the incoming string becomes []byte
2. Create configMap
grammar
func (ConfigMapInterface) Create(ctx , configMap *, opts ) (*, error)
- Syntax example
configMapInfo,err = clientSet.CoreV1().ConfigMaps(namespaceName).Create((),configMap,{})
Complete example
- The configmap of the previous nginx service on k8s is as follows
apiVersion: v1 kind: ConfigMap metadata: name: nginxconf namespace: test data: : | worker_processes 1; events { worker_connections 1024; } http { include ; default_type application/octet-stream; client_max_body_size 50m; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root /usr/share/nginx/html; location / { index ; } } }
- Create a function
package crowK8S import ( "context" coreV1 "/api/core/v1" metaV1 "/apimachinery/pkg/apis/meta/v1" "/client-go/kubernetes" ) func CreateConfigMap(clientSet *,namespaceName string,configMapName string,dataInfo string)(configMapInfo *,err error) { configMap := &{ ObjectMeta: { Name: configMapName, }, Data: map[string]string{ "" : dataInfo, }, } configMapInfo,err = clientSet.CoreV1().ConfigMaps(namespaceName).Create((),configMap,{}) if err != nil { return configMapInfo,err } return configMapInfo,nil }
- Calling functions
package main import ( "fmt" "go-k8s/crowK8S" ) func main() { clientSet,err := crowK8S.ConnectK8s() if err !=nil { (err) } var dataInfo string dataInfo = " worker_processes 1;\n events {\n worker_connections 1024;\n }\n http {\n include ;\n default_type application/octet-stream;\n client_max_body_size 50m;\n sendfile on;\n keepalive_timeout 65;\n server {\n listen 80;\n server_name localhost;\n root /usr/share/nginx/html;\n location / {\n index ;\n }\n }\n }" configMapInfo,err := (clientSet ,"liubei","nginxconf",dataInfo) (configMapInfo) }
- The results are as follows on k8s
[root@crust-m01 ~]# kubectl describe -n liubei configmaps nginxconf Name: nginxconf Namespace: liubei Labels: <none> Annotations: <none> Data ==== : ---- worker_processes 1; events { worker_connections 1024; } http { include ; default_type application/octet-stream; client_max_body_size 50m; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; root /usr/share/nginx/html; location / { index ; } } } Events: <none>
3. Get ConfigMapList
Complete example
- Define functions
package crowK8S import ( "context" coreV1 "/api/core/v1" metaV1 "/apimachinery/pkg/apis/meta/v1" "/client-go/kubernetes" ) func GetConfigMapList(clientSet *,namespaceName string)(configMapList *,err error) { configMapList,err = clientSet.CoreV1().ConfigMaps(namespaceName).List((), {}) if err != nil{ return nil, err } return configMapList, err }
- Using functions
package main import ( "fmt" "go-k8s/crowK8S" ) func main() { clientSet,err := crowK8S.ConnectK8s() if err !=nil { (err) } configMapList,err := (clientSet ,"liubei") if err != nil { (err) } (configMapList) }
Results Print
&ConfigMapList{ListMeta:{/api/v1/namespaces/liubei/configmaps 22893420 <nil>},Items:[]ConfigMap{ConfigMap{ObjectMeta:{ liubei /api/v1/namespaces/liubei/configmaps/ ecb54dbb-3082-4caa-9055-8061e5d9d7b6 19106476 0 2022-09-28 13:23:29 +0800 CST <nil> <nil> map[] map[] [] [] [{kube-controller-manager Update v1 2022-09-28 13:23:29 +0800 CST FieldsV1 {"f:data":{".":{},"f:":{}}} }]},Data:map[string]string{: -----BEGIN CERTIFICATE-----
MIIC6TCCAdGgAwIBAgIBADANBgkqhkiG9w0BAQsFADAVMRMwEQYDVQQDEwprdWJl
cm5ldGVzMCAXDTIyMDcxMjA4NDExNFoYDzIxMjIwNjE4MDg0MTE0WjAVMRMwEQYD
VQQDEwprdWJlcm5ldGVzMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA
85IYxSiow4zNifU1yqMK6knWrJIErQXD6zHUgpAk2Z/c3XfpwONCkTObLEhXJKeN
9wjqOAxx9OLFSqZdefnOjSKw6jZJFC6APLM/bdsX4ECnlg32edQ05iUZxPYZjpdS
BhpbdK4jCirB/XMgdmJizxoR1NHBZNHGbnH0rabfF/PrVrZQdUJpLpoAvOyT3bWr
+HPSHA7mzODAko/RtVGyGoZClBZbFds7f1cyY2JGOB6GqrJMmLVf3xBVGwUO3KLA
0lZ/rfPrS9fEzAD6y1pqke7wr9agrFXWhFZLtwIVqGrt6Zzrq0jxamwPqZsYAXPm
jA3LYX0VnseIJTGX0S9HKQIDAQABo0IwQDAOBgNVHQ8BAf8EBAMCAqQwDwYDVR0T
AQH/BAUwAwEB/zAdBgNVHQ4EFgQUExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hvcNAQELBQADggEBANgTtUROUKrLcihbTJXrhZKo94Q/WX7AeRVz7HlWTEqWFsX1
eZyFNMPeFoswLwGe4nwuS2Nd+WvE+WPZ/0CF+q8/0oGE6B87zdTnJJELTnIqnWIm
k+ac7gMokk7EaCv30FBDX239E++zVooWsHj3Tc1dmn2AY+whgNXnxT9TGNst9o2z
DTlzI2VWg8kay3IhZS0NjsKk1YMbd8c+5uLQZwWEtGa7HlD8ooOF/emOINVIbRH4
T7LiVjQH3JJPZtYSWnl88IMtXlW360oABkVdKY4Z1nNzrNWBCGOFQ4Y75XmFY6Qi
2c0f8L2WtTFdrXgbbHCbOaIj9rruEH5wKxjxBg8=
-----END CERTIFICATE-----
,},BinaryData:map[string][]byte{},Immutable:nil,},ConfigMap{ObjectMeta:{nginxconf liubei /api/v1/namespaces/liubei/configmaps/nginxconf 395535db-1df7-408a-9976-18e6ded1207e 22854355 0 2022-10-12 17:21:03 +0800 CST <nil> <nil> map[] map[] [] [] [{___go_build_main_go.exe Update v1 2022-10-12 17:21:03 +0800 CST FieldsV1 {"f:data":{".":{},"f:":{}}} }]},Data:map[string]string{: worker_processes 1;
events {
worker_connections 1024;
}
http {
include ;
default_type application/octet-stream;
client_max_body_size 50m;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index ;
}
}
},},BinaryData:map[string][]byte{},Immutable:nil,},},}
4. Get ConfigMap
func (ConfigMapInterface) Get(ctx , name string, opts ) (*, error)
- Syntax example
configmapInfo,err = clientSet.CoreV1().ConfigMaps(namespaceName).Get((),configMapName,{})
Complete example
- Create a function
package crowK8S import ( "context" coreV1 "/api/core/v1" metaV1 "/apimachinery/pkg/apis/meta/v1" "/client-go/kubernetes" ) func GetConfigMap(clientSet *,namespaceName string,configMapName string)(configmapInfo *,err error) { configmapInfo,err = clientSet.CoreV1().ConfigMaps(namespaceName).Get((), configMapName,{}) if err != nil{ return nil, err } return configmapInfo, err }
- Call
package main import ( "fmt" "go-k8s/crowK8S" ) func main() { clientSet,err := crowK8S.ConnectK8s() if err !=nil { (err) } configmapInfo,err := (clientSet ,"liubei","nginxconf") if err != nil { (err) } (configmapInfo) }
result
&ConfigMap{ObjectMeta:{nginxconf liubei /api/v1/namespaces/liubei/configmaps/nginxconf 395535db-1df7-408a-9976-18e6ded1207e 22854355 0 2022-10-12 17:21:03 +0800 CST <nil> <nil> map[] map[] [] [] [{___go_build_main_go.exe Update v1 2022-10-12 17:21:03 +0800 CST FieldsV1 {"f:data":{".":{},"f:":{}}} }]},Data:map[string]string{: worker_processes 1;
events {
worker_connections 1024;
}
http {
include ;
default_type application/octet-stream;
client_max_body_size 50m;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
location / {
index ;
}
}
},},BinaryData:map[string][]byte{},Immutable:nil,}
5. Update ConfigMap
func (ConfigMapInterface) Update(ctx , configMap *, opts ) (*, error)
- Syntax example
configmapInfo,err = clientSet.CoreV1().ConfigMaps(namespaceName).Update((),configmapInfo,{})
Complete example
- Create a function
package crowK8S import ( "context" coreV1 "/api/core/v1" metaV1 "/apimachinery/pkg/apis/meta/v1" "/client-go/kubernetes" ) func ApplyConfigMap(clientSet *,namespaceName string,configMapName string,fileName string,configMapData string)(configmapInfo *,err error) { configmapInfo,err = clientSet.CoreV1().ConfigMaps(namespaceName).Get((), configMapName,{}) if err != nil{ return nil, err } [fileName] = configMapData configmapInfo,err = clientSet.CoreV1().ConfigMaps(namespaceName).Update((),configmapInfo,{}) if err !=nil { return configmapInfo,err } return configmapInfo,nil }
- Call
func main() { clientSet,err := crowK8S.ConnectK8s() if err !=nil { (err) } configMapInfo,err := (clientSet ,"liubei","nginxconf","","hello world") (configMapInfo) }
- Results Print
&ConfigMap{ObjectMeta:{nginxconf liubei /api/v1/namespaces/liubei/configmaps/nginxconf 395535db-1df7-408a-9976-18e6ded1207e 22901163 0 2022-10-12 17:21:03 +0800 CST <nil> <nil> map[] map[] [] [] [{___go_build_main_go.exe Update v1 2022-10-12 17:21:03 +0800 CST FieldsV1 {"f:data":{".":{},"f:":{}}} }]},Data:map[string]string{: hello world,},BinaryData:map[string][]byte{},Immutable:nil,}
- View results on k8s
[root@crust-m01 ~]# kubectl describe -n liubei configmaps nginxconf Name: nginxconf Namespace: liubei Labels: <none> Annotations: <none> Data ==== : ---- hello world Events: <none>
6. Delete ConfigMap
func (ConfigMapInterface) Delete(ctx , name string, opts ) error
- Syntax example
err = clientSet.CoreV1().ConfigMaps(namespaceName).Delete((),configMapName,{})
Complete example
- Create a function
package crowK8S import ( "context" coreV1 "/api/core/v1" metaV1 "/apimachinery/pkg/apis/meta/v1" "/client-go/kubernetes" ) func DeleteConfigMap(clientSet *,namespaceName string,configMapName string)(err error) { err = clientSet.CoreV1().ConfigMaps(namespaceName).Delete((),configMapName,{}) if err != nil { return err } return nil }
- Call
package main import ( "fmt" "go-k8s/crowK8S" ) func main() { clientSet,err := crowK8S.ConnectK8s() if err !=nil { (err) } err = (clientSet,"liubei","nginxconf") if err != nil { (err) }else { ("Delete successfully") } }
Results Print
Delete successfully
The above is the detailed content of the ConfigMap operation analysis of Go language development k8s. For more information about Go development k8s ConfigMap operation, please follow my other related articles!