diff --git a/lib/kubetruth/kubeapi.rb b/lib/kubetruth/kubeapi.rb index 5ec385c..5eb0954 100644 --- a/lib/kubetruth/kubeapi.rb +++ b/lib/kubetruth/kubeapi.rb @@ -56,7 +56,7 @@ def ensure_namespace(ns = namespace) rescue Kubeclient::ResourceNotFoundError newns = Kubeclient::Resource.new newns.metadata = {} - newns.metadata.name = namespace + newns.metadata.name = ns set_managed(newns) client.create_namespace(newns) end diff --git a/spec/fixtures/vcr/Kubetruth_KubeApi/ensure_namespace/creates_namespace_if_not_present.yml b/spec/fixtures/vcr/Kubetruth_KubeApi/ensure_namespace/creates_namespace_if_not_present.yml index c7d7ffb..0c38e43 100644 --- a/spec/fixtures/vcr/Kubetruth_KubeApi/ensure_namespace/creates_namespace_if_not_present.yml +++ b/spec/fixtures/vcr/Kubetruth_KubeApi/ensure_namespace/creates_namespace_if_not_present.yml @@ -31,7 +31,7 @@ http_interactions: X-Kubernetes-Pf-Prioritylevel-Uid: - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 Date: - - Wed, 16 Jun 2021 15:28:39 GMT + - Thu, 17 Jun 2021 20:08:13 GMT Transfer-Encoding: - chunked body: @@ -39,7 +39,7 @@ http_interactions: string: '{"kind":"APIResourceList","groupVersion":"v1","resources":[{"name":"bindings","singularName":"","namespaced":true,"kind":"Binding","verbs":["create"]},{"name":"componentstatuses","singularName":"","namespaced":false,"kind":"ComponentStatus","verbs":["get","list"],"shortNames":["cs"]},{"name":"configmaps","singularName":"","namespaced":true,"kind":"ConfigMap","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["cm"],"storageVersionHash":"qFsyl6wFWjQ="},{"name":"endpoints","singularName":"","namespaced":true,"kind":"Endpoints","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["ep"],"storageVersionHash":"fWeeMqaN/OA="},{"name":"events","singularName":"","namespaced":true,"kind":"Event","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["ev"],"storageVersionHash":"r2yiGXH7wu8="},{"name":"limitranges","singularName":"","namespaced":true,"kind":"LimitRange","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["limits"],"storageVersionHash":"EBKMFVe6cwo="},{"name":"namespaces","singularName":"","namespaced":false,"kind":"Namespace","verbs":["create","delete","get","list","patch","update","watch"],"shortNames":["ns"],"storageVersionHash":"Q3oi5N2YM8M="},{"name":"namespaces/finalize","singularName":"","namespaced":false,"kind":"Namespace","verbs":["update"]},{"name":"namespaces/status","singularName":"","namespaced":false,"kind":"Namespace","verbs":["get","patch","update"]},{"name":"nodes","singularName":"","namespaced":false,"kind":"Node","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["no"],"storageVersionHash":"XwShjMxG9Fs="},{"name":"nodes/proxy","singularName":"","namespaced":false,"kind":"NodeProxyOptions","verbs":["create","delete","get","patch","update"]},{"name":"nodes/status","singularName":"","namespaced":false,"kind":"Node","verbs":["get","patch","update"]},{"name":"persistentvolumeclaims","singularName":"","namespaced":true,"kind":"PersistentVolumeClaim","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["pvc"],"storageVersionHash":"QWTyNDq0dC4="},{"name":"persistentvolumeclaims/status","singularName":"","namespaced":true,"kind":"PersistentVolumeClaim","verbs":["get","patch","update"]},{"name":"persistentvolumes","singularName":"","namespaced":false,"kind":"PersistentVolume","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["pv"],"storageVersionHash":"HN/zwEC+JgM="},{"name":"persistentvolumes/status","singularName":"","namespaced":false,"kind":"PersistentVolume","verbs":["get","patch","update"]},{"name":"pods","singularName":"","namespaced":true,"kind":"Pod","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["po"],"categories":["all"],"storageVersionHash":"xPOwRZ+Yhw8="},{"name":"pods/attach","singularName":"","namespaced":true,"kind":"PodAttachOptions","verbs":["create","get"]},{"name":"pods/binding","singularName":"","namespaced":true,"kind":"Binding","verbs":["create"]},{"name":"pods/eviction","singularName":"","namespaced":true,"group":"policy","version":"v1beta1","kind":"Eviction","verbs":["create"]},{"name":"pods/exec","singularName":"","namespaced":true,"kind":"PodExecOptions","verbs":["create","get"]},{"name":"pods/log","singularName":"","namespaced":true,"kind":"Pod","verbs":["get"]},{"name":"pods/portforward","singularName":"","namespaced":true,"kind":"PodPortForwardOptions","verbs":["create","get"]},{"name":"pods/proxy","singularName":"","namespaced":true,"kind":"PodProxyOptions","verbs":["create","delete","get","patch","update"]},{"name":"pods/status","singularName":"","namespaced":true,"kind":"Pod","verbs":["get","patch","update"]},{"name":"podtemplates","singularName":"","namespaced":true,"kind":"PodTemplate","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"storageVersionHash":"LIXB2x4IFpk="},{"name":"replicationcontrollers","singularName":"","namespaced":true,"kind":"ReplicationController","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["rc"],"categories":["all"],"storageVersionHash":"Jond2If31h0="},{"name":"replicationcontrollers/scale","singularName":"","namespaced":true,"group":"autoscaling","version":"v1","kind":"Scale","verbs":["get","patch","update"]},{"name":"replicationcontrollers/status","singularName":"","namespaced":true,"kind":"ReplicationController","verbs":["get","patch","update"]},{"name":"resourcequotas","singularName":"","namespaced":true,"kind":"ResourceQuota","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["quota"],"storageVersionHash":"8uhSgffRX6w="},{"name":"resourcequotas/status","singularName":"","namespaced":true,"kind":"ResourceQuota","verbs":["get","patch","update"]},{"name":"secrets","singularName":"","namespaced":true,"kind":"Secret","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"storageVersionHash":"S6u1pOWzb84="},{"name":"serviceaccounts","singularName":"","namespaced":true,"kind":"ServiceAccount","verbs":["create","delete","deletecollection","get","list","patch","update","watch"],"shortNames":["sa"],"storageVersionHash":"pbx9ZvyFpBE="},{"name":"serviceaccounts/token","singularName":"","namespaced":true,"group":"authentication.k8s.io","version":"v1","kind":"TokenRequest","verbs":["create"]},{"name":"services","singularName":"","namespaced":true,"kind":"Service","verbs":["create","delete","get","list","patch","update","watch"],"shortNames":["svc"],"categories":["all"],"storageVersionHash":"0/CO1lhkEBI="},{"name":"services/proxy","singularName":"","namespaced":true,"kind":"ServiceProxyOptions","verbs":["create","delete","get","patch","update"]},{"name":"services/status","singularName":"","namespaced":true,"kind":"Service","verbs":["get","patch","update"]}]} ' - recorded_at: Wed, 16 Jun 2021 15:28:39 GMT + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT - request: method: get uri: https://127.0.0.1:60761/api/v1/namespaces/kubetruth-test-ns-newns @@ -71,7 +71,7 @@ http_interactions: X-Kubernetes-Pf-Prioritylevel-Uid: - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 Date: - - Wed, 16 Jun 2021 15:28:39 GMT + - Thu, 17 Jun 2021 20:08:13 GMT Content-Length: - '226' body: @@ -80,7 +80,7 @@ http_interactions: \"kubetruth-test-ns-newns\" not found","reason":"NotFound","details":{"name":"kubetruth-test-ns-newns","kind":"namespaces"},"code":404} ' - recorded_at: Wed, 16 Jun 2021 15:28:39 GMT + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT - request: method: get uri: https://127.0.0.1:60761/api/v1/namespaces/kubetruth-test-ns-newns @@ -112,7 +112,7 @@ http_interactions: X-Kubernetes-Pf-Prioritylevel-Uid: - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 Date: - - Wed, 16 Jun 2021 15:28:39 GMT + - Thu, 17 Jun 2021 20:08:13 GMT Content-Length: - '226' body: @@ -121,7 +121,7 @@ http_interactions: \"kubetruth-test-ns-newns\" not found","reason":"NotFound","details":{"name":"kubetruth-test-ns-newns","kind":"namespaces"},"code":404} ' - recorded_at: Wed, 16 Jun 2021 15:28:39 GMT + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT - request: method: post uri: https://127.0.0.1:60761/api/v1/namespaces @@ -157,15 +157,15 @@ http_interactions: X-Kubernetes-Pf-Prioritylevel-Uid: - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 Date: - - Wed, 16 Jun 2021 15:28:39 GMT + - Thu, 17 Jun 2021 20:08:13 GMT Content-Length: - - '565' + - '566' body: encoding: UTF-8 - string: '{"kind":"Namespace","apiVersion":"v1","metadata":{"name":"kubetruth-test-ns-newns","uid":"c64707d8-cf5b-4906-bc31-0794832b06b0","resourceVersion":"45357","creationTimestamp":"2021-06-16T15:28:39Z","labels":{"app.kubernetes.io/managed-by":"kubetruth"},"managedFields":[{"manager":"rest-client","operation":"Update","apiVersion":"v1","time":"2021-06-16T15:28:39Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:labels":{".":{},"f:app.kubernetes.io/managed-by":{}}},"f:status":{"f:phase":{}}}}]},"spec":{"finalizers":["kubernetes"]},"status":{"phase":"Active"}} + string: '{"kind":"Namespace","apiVersion":"v1","metadata":{"name":"kubetruth-test-ns-newns","uid":"e3d26db8-a292-4542-b161-1a9879967626","resourceVersion":"116718","creationTimestamp":"2021-06-17T20:08:13Z","labels":{"app.kubernetes.io/managed-by":"kubetruth"},"managedFields":[{"manager":"rest-client","operation":"Update","apiVersion":"v1","time":"2021-06-17T20:08:13Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:labels":{".":{},"f:app.kubernetes.io/managed-by":{}}},"f:status":{"f:phase":{}}}}]},"spec":{"finalizers":["kubernetes"]},"status":{"phase":"Active"}} ' - recorded_at: Wed, 16 Jun 2021 15:28:39 GMT + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT - request: method: get uri: https://127.0.0.1:60761/api/v1/namespaces/kubetruth-test-ns-newns @@ -197,13 +197,179 @@ http_interactions: X-Kubernetes-Pf-Prioritylevel-Uid: - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 Date: - - Wed, 16 Jun 2021 15:28:39 GMT + - Thu, 17 Jun 2021 20:08:13 GMT Content-Length: - - '565' + - '566' body: encoding: UTF-8 - string: '{"kind":"Namespace","apiVersion":"v1","metadata":{"name":"kubetruth-test-ns-newns","uid":"c64707d8-cf5b-4906-bc31-0794832b06b0","resourceVersion":"45357","creationTimestamp":"2021-06-16T15:28:39Z","labels":{"app.kubernetes.io/managed-by":"kubetruth"},"managedFields":[{"manager":"rest-client","operation":"Update","apiVersion":"v1","time":"2021-06-16T15:28:39Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:labels":{".":{},"f:app.kubernetes.io/managed-by":{}}},"f:status":{"f:phase":{}}}}]},"spec":{"finalizers":["kubernetes"]},"status":{"phase":"Active"}} + string: '{"kind":"Namespace","apiVersion":"v1","metadata":{"name":"kubetruth-test-ns-newns","uid":"e3d26db8-a292-4542-b161-1a9879967626","resourceVersion":"116718","creationTimestamp":"2021-06-17T20:08:13Z","labels":{"app.kubernetes.io/managed-by":"kubetruth"},"managedFields":[{"manager":"rest-client","operation":"Update","apiVersion":"v1","time":"2021-06-17T20:08:13Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:labels":{".":{},"f:app.kubernetes.io/managed-by":{}}},"f:status":{"f:phase":{}}}}]},"spec":{"finalizers":["kubernetes"]},"status":{"phase":"Active"}} ' - recorded_at: Wed, 16 Jun 2021 15:28:39 GMT + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT +- request: + method: get + uri: https://127.0.0.1:60761/api/v1/namespaces/kubetruth-test-ns-newns2 + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin20 x86_64) ruby/2.7.3p183 + Authorization: + - Bearer + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - 127.0.0.1:60761 + response: + status: + code: 404 + message: Not Found + headers: + Cache-Control: + - no-cache, private + Content-Type: + - application/json + X-Kubernetes-Pf-Flowschema-Uid: + - c6d9dbd7-dd7c-4844-9970-a2c737a707e7 + X-Kubernetes-Pf-Prioritylevel-Uid: + - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 + Date: + - Thu, 17 Jun 2021 20:08:13 GMT + Content-Length: + - '228' + body: + encoding: UTF-8 + string: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"namespaces + \"kubetruth-test-ns-newns2\" not found","reason":"NotFound","details":{"name":"kubetruth-test-ns-newns2","kind":"namespaces"},"code":404} + + ' + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT +- request: + method: get + uri: https://127.0.0.1:60761/api/v1/namespaces/kubetruth-test-ns-newns2 + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin20 x86_64) ruby/2.7.3p183 + Authorization: + - Bearer + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - 127.0.0.1:60761 + response: + status: + code: 404 + message: Not Found + headers: + Cache-Control: + - no-cache, private + Content-Type: + - application/json + X-Kubernetes-Pf-Flowschema-Uid: + - c6d9dbd7-dd7c-4844-9970-a2c737a707e7 + X-Kubernetes-Pf-Prioritylevel-Uid: + - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 + Date: + - Thu, 17 Jun 2021 20:08:13 GMT + Content-Length: + - '228' + body: + encoding: UTF-8 + string: '{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"namespaces + \"kubetruth-test-ns-newns2\" not found","reason":"NotFound","details":{"name":"kubetruth-test-ns-newns2","kind":"namespaces"},"code":404} + + ' + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT +- request: + method: post + uri: https://127.0.0.1:60761/api/v1/namespaces + body: + encoding: UTF-8 + string: '{"metadata":{"name":"kubetruth-test-ns-newns2","labels":{"app.kubernetes.io/managed-by":"kubetruth"}},"kind":"Namespace","apiVersion":"v1"}' + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin20 x86_64) ruby/2.7.3p183 + Content-Type: + - application/json + Authorization: + - Bearer + Content-Length: + - '139' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - 127.0.0.1:60761 + response: + status: + code: 201 + message: Created + headers: + Cache-Control: + - no-cache, private + Content-Type: + - application/json + X-Kubernetes-Pf-Flowschema-Uid: + - c6d9dbd7-dd7c-4844-9970-a2c737a707e7 + X-Kubernetes-Pf-Prioritylevel-Uid: + - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 + Date: + - Thu, 17 Jun 2021 20:08:13 GMT + Content-Length: + - '567' + body: + encoding: UTF-8 + string: '{"kind":"Namespace","apiVersion":"v1","metadata":{"name":"kubetruth-test-ns-newns2","uid":"60ecc692-9cfc-428b-b54b-af3fbea46bbf","resourceVersion":"116723","creationTimestamp":"2021-06-17T20:08:13Z","labels":{"app.kubernetes.io/managed-by":"kubetruth"},"managedFields":[{"manager":"rest-client","operation":"Update","apiVersion":"v1","time":"2021-06-17T20:08:13Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:labels":{".":{},"f:app.kubernetes.io/managed-by":{}}},"f:status":{"f:phase":{}}}}]},"spec":{"finalizers":["kubernetes"]},"status":{"phase":"Active"}} + + ' + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT +- request: + method: get + uri: https://127.0.0.1:60761/api/v1/namespaces/kubetruth-test-ns-newns2 + body: + encoding: US-ASCII + string: '' + headers: + Accept: + - "*/*" + User-Agent: + - rest-client/2.1.0 (darwin20 x86_64) ruby/2.7.3p183 + Authorization: + - Bearer + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Host: + - 127.0.0.1:60761 + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - no-cache, private + Content-Type: + - application/json + X-Kubernetes-Pf-Flowschema-Uid: + - c6d9dbd7-dd7c-4844-9970-a2c737a707e7 + X-Kubernetes-Pf-Prioritylevel-Uid: + - 3bc95a2c-9c78-4c16-8ee2-9c0b68ead8f6 + Date: + - Thu, 17 Jun 2021 20:08:13 GMT + Content-Length: + - '567' + body: + encoding: UTF-8 + string: '{"kind":"Namespace","apiVersion":"v1","metadata":{"name":"kubetruth-test-ns-newns2","uid":"60ecc692-9cfc-428b-b54b-af3fbea46bbf","resourceVersion":"116723","creationTimestamp":"2021-06-17T20:08:13Z","labels":{"app.kubernetes.io/managed-by":"kubetruth"},"managedFields":[{"manager":"rest-client","operation":"Update","apiVersion":"v1","time":"2021-06-17T20:08:13Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:labels":{".":{},"f:app.kubernetes.io/managed-by":{}}},"f:status":{"f:phase":{}}}}]},"spec":{"finalizers":["kubernetes"]},"status":{"phase":"Active"}} + + ' + recorded_at: Thu, 17 Jun 2021 20:08:13 GMT recorded_with: VCR 6.0.0 diff --git a/spec/kubetruth/kubeapi_spec.rb b/spec/kubetruth/kubeapi_spec.rb index 7334610..c5540ab 100644 --- a/spec/kubetruth/kubeapi_spec.rb +++ b/spec/kubetruth/kubeapi_spec.rb @@ -106,6 +106,13 @@ def apiserver; "https://127.0.0.1"; end ns = kapi.client.get_namespace(kapi.namespace) expect(ns.kind).to eq("Namespace") expect(ns.metadata.name).to eq(kapi.namespace) + + newns = "#{namespace}-newns2" + expect { kapi.client.get_namespace(newns) }.to raise_error(Kubeclient::ResourceNotFoundError, /namespaces.*not found/) + kapi.ensure_namespace(newns) + ns = kapi.client.get_namespace(newns) + expect(ns.kind).to eq("Namespace") + expect(ns.metadata.name).to eq(newns) end it "sets labels when creating namespace" do