Skip to content

Commit 442d67a

Browse files
committed
kafka operator installation, working on #32
1 parent 4986a38 commit 442d67a

File tree

4 files changed

+372
-15
lines changed

4 files changed

+372
-15
lines changed

k8s/k8ssetup/k8ssetup.go

+63-7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ type K8sSetUp interface {
1818
Initialize() error
1919
InstallPostgresqlOperator() error
2020
DatabaseCreation(fileName string) error
21+
InstallKafkaOperator() error
2122
}
2223

2324
type k8sSetUpImpl struct {
@@ -50,6 +51,23 @@ func (k k8sSetUpImpl) InstallPostgresqlOperator() error {
5051
return nil
5152
}
5253

54+
func (k k8sSetUpImpl) InstallKafkaOperator() error {
55+
log.Println("Installing Kafka operator ...")
56+
57+
if installed := k.isKafkaOperatorInstalled(); !installed {
58+
log.Println("Kafka operator not installed ...")
59+
if err := k.doKafkaOperatorInstallation(); err != nil {
60+
return fmt.Errorf("error installing Kafka operator: %v", err)
61+
}
62+
k.waiKafkaOperatorRunning()
63+
64+
} else {
65+
log.Println("Kafka operator is installed ...")
66+
}
67+
68+
return nil
69+
}
70+
5371
func (k k8sSetUpImpl) waitPsqlOperatorRunning() {
5472
cnt := true
5573
for cnt {
@@ -59,24 +77,41 @@ func (k k8sSetUpImpl) waitPsqlOperatorRunning() {
5977
log.Print("Psql operator is running")
6078
}
6179

62-
func (k k8sSetUpImpl) isPsqlOperatorRunning() (running bool, err error) {
63-
log.Printf("Checking if postgres operator is already running ...")
80+
func (k k8sSetUpImpl) waiKafkaOperatorRunning() {
81+
cnt := true
82+
for cnt {
83+
ready, err := k.isKafkaOperatorRunning()
84+
cnt = !(err == nil && ready)
85+
}
86+
log.Print("Kafka operator is running")
87+
}
88+
89+
func (k k8sSetUpImpl) isOperatorRunning(name, namespace string) (running bool, err error) {
90+
log.Printf("Checking if %s operator is already running ...", name)
6491

6592
var podNames, output string
66-
if podNames, err = k.kubectl("get", "pod", "-o", "name"); err == nil {
67-
for _, name := range strings.Split(podNames, "\n") {
68-
if strings.Contains(name, "postgres-operator") {
69-
if output, err = k.kubectl("get", name, "-o", "jsonpath='{.status.phase}'"); err != nil {
93+
if podNames, err = k.kubectl("get", "pod", "-o", "name", "-n", namespace); err == nil {
94+
for _, podName := range strings.Split(podNames, "\n") {
95+
if strings.Contains(podName, name) {
96+
if output, err = k.kubectl("get", podName, "-o", "jsonpath='{.status.containerStatuses[0].ready}'", "-n", namespace); err != nil {
7097
return false, err
7198
}
72-
running = output == "'Running'"
99+
running = output == "'true'"
73100
break
74101
}
75102
}
76103
}
77104
return
78105
}
79106

107+
func (k k8sSetUpImpl) isPsqlOperatorRunning() (bool, error) {
108+
return k.isOperatorRunning("postgres-operator", "default")
109+
}
110+
111+
func (k k8sSetUpImpl) isKafkaOperatorRunning() (bool, error) {
112+
return k.isOperatorRunning("strimzi-cluster-operator", "kafka")
113+
}
114+
80115
func (k k8sSetUpImpl) isPostgreSQLOperatorInstalled() bool {
81116
log.Println("Checking if postgresql operator is already installed ...")
82117
if _, err := k.kubectl("describe", "service/postgres-operator"); err != nil {
@@ -86,6 +121,15 @@ func (k k8sSetUpImpl) isPostgreSQLOperatorInstalled() bool {
86121
return true
87122
}
88123

124+
func (k k8sSetUpImpl) isKafkaOperatorInstalled() bool {
125+
log.Println("Checking if kafka operator is already installed ...")
126+
if _, err := k.kubectl("describe", "deployment.apps/strimzi-cluster-operator", "-n", "kafka"); err != nil {
127+
return false
128+
}
129+
130+
return true
131+
}
132+
89133
func (k *k8sSetUpImpl) doPsqlOperatorInstallation() error {
90134
log.Println("Installing postgreSQL operator ...")
91135
dir, err := ioutil.TempDir("", "pets-go-infra")
@@ -120,6 +164,18 @@ func (k *k8sSetUpImpl) doPsqlOperatorInstallation() error {
120164
return nil
121165
}
122166

167+
func (k *k8sSetUpImpl) doKafkaOperatorInstallation() error {
168+
log.Println("Installing kafka operator ...")
169+
if _, err := k.kubectl("create", "namespace", "kafka"); err != nil {
170+
return fmt.Errorf("error in kubectl create: %v", err)
171+
}
172+
if _, err := k.kubectl("apply", "-f", "https://strimzi.io/install/latest?namespace=kafka", "-n", "kafka"); err != nil {
173+
return fmt.Errorf("error in kubectl apply: %v", err)
174+
}
175+
176+
return nil
177+
}
178+
123179
func (k k8sSetUpImpl) kubectl(params ...string) (output string, err error) {
124180
return k.executeCommand(k.kubectlPath, params...)
125181
}

0 commit comments

Comments
 (0)