summaryrefslogtreecommitdiff
path: root/qa/qa/service/cluster_provider/k3s_cilium.rb
blob: 5b529caa20bd8b0758a629089748d3160e9c544a (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true

module QA
  module Service
    module ClusterProvider
      class K3sCilium < K3s
        def setup
          @k3s = Service::DockerRun::K3s.new.tap do |k3s|
            k3s.remove!
            k3s.cni_enabled = true
            k3s.register!

            shell "kubectl config set-cluster k3s --server https://#{k3s.host_name}:6443 --insecure-skip-tls-verify"
            shell 'kubectl config set-credentials default --username=node --password=some-secret'
            shell 'kubectl config set-context k3s --cluster=k3s --user=default'
            shell 'kubectl config use-context k3s'

            wait_for_server(k3s.host_name) do
              shell 'kubectl version'
              # install local storage
              shell 'kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/master/deploy/local-path-storage.yaml'

              # patch local storage
              shell %(kubectl patch storageclass local-path -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}')
              shell 'kubectl create -f https://raw.githubusercontent.com/cilium/cilium/v1.8/install/kubernetes/quick-install.yaml'

              wait_for_namespaces do
                wait_for_cilium
                wait_for_coredns do
                  shell 'kubectl create -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-0.31.0/deploy/static/provider/cloud/deploy.yaml'
                  wait_for_ingress
                end
              end
            end
          end
        end

        private

        def wait_for_cilium
          QA::Runtime::Logger.info 'Waiting for Cilium pod to be initialized'

          60.times do
            if service_available?('kubectl get pods --all-namespaces -l k8s-app=cilium --no-headers=true | grep -o "cilium-.*1/1"')
              return yield if block_given?

              return true
            end

            sleep 1
            QA::Runtime::Logger.info '.'
          end

          raise 'Cilium pod has not initialized correctly'
        end

        def wait_for_coredns
          QA::Runtime::Logger.info 'Waiting for CoreDNS pod to be initialized'

          60.times do
            if service_available?('kubectl get pods --all-namespaces --no-headers=true | grep -o "coredns.*1/1"')
              return yield if block_given?

              return true
            end

            sleep 1
            QA::Runtime::Logger.info '.'
          end

          raise 'CoreDNS pod has not been initialized correctly'
        end

        def wait_for_ingress
          QA::Runtime::Logger.info 'Waiting for Ingress controller pod to be initialized'

          60.times do
            if service_available?('kubectl get pods --all-namespaces -l app.kubernetes.io/component=controller | grep -o "ingress-nginx-controller.*1/1"')
              return yield if block_given?

              return true
            end

            sleep 1
            QA::Runtime::Logger.info '.'
          end

          raise 'Ingress pod has not been initialized correctly'
        end
      end
    end
  end
end