티스토리 뷰

난 주로 openshift를 사용하기 때문에 oc 클라이언트로 cli 를 날린다고 가정한다. 하지만 kubectl을 써도 사실은 동일하니 큰 차이는 없음

 

yaml에서 데이터 타입 별 추출

  - 일반 값

    예를 들어 오브젝트가 아래와 같은 yaml 구조를 보인다고 가정하자. - OpenShift의 Project(namespace)

    여기서 metadata.name 은 일반 값이다. 즉 key:value 가 1:1 맵핑이다.

apiVersion: project.openshift.io/v1
kind: Project
metadata:
  annotations:
    openshift.io/display-name: XXXX
    openshift.io/requester: system:admin
    openshift.io.sa.scc.mcs: s0:c18,c7
    openshift.io/sa.scc.supplemental-groups: 1000320000/10000
    openshift.io/sa.scc.uid-range: 1000320000/10000
  creationTimeStamp: xxxx-xx-xxxxxx:xx:xxx
  name: xxxxxx
  resource.Version: ...
  ...
spec:
  ....
status:
  ....

  - How to retreive

    오브젝트 하나를 지정하는 경우

$ oc get project xxxx -o go-template='{{.metadata.name}}'

     여러개의 오브젝트 목록을 모두 찾는 경우

$ oc get project -o go-template='{{range .items}}{{.metadata.name}}{{end}}'

 

  - Map

    위 예에서 metadata.annotations 는 Map 이다. 즉 여러개의 key:value 세트를 가진다. 예를 들어, annotations["openshift.io/display-name"] 은 "XXXX" 이다.

metadata:
  annotations:
    openshift.io/display-name: XXXX
    openshift.io/requester: system:admin
    openshift.io.sa.scc.mcs: s0:c18,c7
    openshift.io/sa.scc.supplemental-groups: 1000320000/10000
    openshift.io/sa.scc.uid-range: 1000320000/10000

  - How to retreive

    index 함수를 사용한다. index "path" "key" 형태로 구성한다.

    오브젝트 하나를 지정하는 경우

$ oc get project xxxx -o go-template='{{index .metadata.annotations "openshift.io/display-name"}}'

     여러개의 오브젝트 목록을 모두 찾는 경우

$ oc get project -o go-template='{{range .items}}{{index .metadata.annotations "openshift.io/display-name"}}'{{end}}'

 

 

  - Array

    배열은 Map 과 달리 "key" 가 없다. 그냥 index 로만 가리키며, 0 부터 시작한다.

    아레  예에서, egressIPs[0] = xxx.xxx.xxx.xxx, egressIPs[1] = yyy.yyy.yyy.yyy 이다.

apiVersion: network.openshift.io/v1
egressIPs:
- xxx.xxx.xxx.xxx
- yyy.yyy.yyy.yyy
.....

  - How to retreive

    index 함수를 사용한다. index "path" "배열인덱스 번호" 형태로 구성한다.

$ oc get netnamespace xxxx -o go-template='{{index egressIPs 0}}'
$ oc get netnamespace xxxx -o go-template='{{index egressIPs 1}}'

 

  - Complex

    Map, Array 혼용이다. 아래는 limitrange 오브젝트의 예이다.

    먼저 Array 형태로 파란색 멤버와 빨간색 멤버 두 개의 값을 가지고 있다.

    파란색 Array 값은 "max", "min", "type" 이라는 key를 가진 Map 으로 구성되어 있다.

    Type 은 일반 String 값 하나만 가지고 있다.

    max는 다시 "cpu", "memory" 라는 key를 가진 Map 으로 구성되어 있다.

....
spec:
  limits:
  - max:
        cpu: "2"
        memory: 4Gi
     min:
        cpu: "1"
        memory: 512Mi
     type: Pod
  - default:
        cpu: "1"
        memory: 2Gi
     defaultRequest:
        cpu: "1"
        memory: 2Gi
     max:
        cpu: "2"
        memory: 4Gi
     maxLimitRequestRatio:
        cpu: "2"
     min:
        cpu: "1"
        memory: 512Mi
     type: Container
      

 

  - How to retreive

    이 경우는 한번에 템플릿으로 retreive 하기는 어렵고 다중 range loop 로 돌리면 된다. 예를 들어, limits의 첫 번째 배열인 type: Pod 의 max cpu를 얻고자 한다면,

$ oc get limitrange xxxx -o go-template='{{range .spec.limits}}{{if eq .type "Pod"}}{{index .max "cpu"}}{{end}}{{end}}'

    약간 보기좋게 풀면

{{range .spec.limits}}        // .spec.limits 내 배열 값에 대해 loop
    {{if eq .type "Pod"}}      // 배열의 type 값이 "Pod" 이면
        {{index .max "cpu"}} // 그 아래 .max 맵에서 "cpu" 키 값을 추출
    {{end}}
{{end}}

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함