Skip to content

Commit 3825190

Browse files
author
Enda Phelan
committed
fix: only store kafka ID in config
1 parent 35864dc commit 3825190

File tree

8 files changed

+39
-27
lines changed

8 files changed

+39
-27
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ Your config should look as follows:
191191
...
192192
"services": {
193193
"kafka": {
194-
"clusterHost": "localhost:9092",
195-
"clusterId": "1iSY6RQ3JKI8Q0OTmjQFd3ocFRg",
196-
"clusterName": "serviceapi"
194+
"clusterId": "1iSY6RQ3JKI8Q0OTmjQFd3ocFRg"
197195
}
198196
}
199197
...

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ k8s.io/apimachinery v0.19.4 h1:+ZoddM7nbzrDCp0T3SWnyxqf8cbWPT2fkZImoyvHUG0=
554554
k8s.io/apimachinery v0.19.4/go.mod h1:DnPGDnARWFvYa3pMHgSxtbZb7gpzzAZ1pTfaUNDVlmA=
555555
k8s.io/client-go v0.19.4 h1:85D3mDNoLF+xqpyE9Dh/OtrJDyJrSRKkHmDXIbEzer8=
556556
k8s.io/client-go v0.19.4/go.mod h1:ZrEy7+wj9PjH5VMBCuu/BDlvtUAku0oVFk4MmnW9mWA=
557+
k8s.io/client-go v1.5.1 h1:XaX/lo2/u3/pmFau8HN+sB5C/b4dc4Dmm2eXjBH4p1E=
558+
k8s.io/client-go v11.0.0+incompatible h1:LBbX2+lOwY9flffWlJM7f1Ct8V2SRNiMRDFeiwnJo9o=
557559
k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
558560
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
559561
k8s.io/klog/v2 v2.2.0 h1:XRvcwJozkgZ1UQJmfMGpvRthQHOvihEhYtDfAaxMz/A=

pkg/cmd/kafka/connect/connect.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,21 @@ func connectToCluster(connection *pkgConnection.Connection) {
135135
}
136136
}
137137

138+
kafkaCfg := clicfg.Services.Kafka
139+
140+
managedservices := connection.NewMASClient()
141+
kafkaInstance, _, err := managedservices.DefaultApi.GetKafkaById(context.TODO(), kafkaCfg.ClusterID)
142+
143+
if err != nil {
144+
fmt.Fprintf(os.Stderr, "Could not get Kafka cluster with ID '%v': %w", kafkaCfg.ClusterID)
145+
}
146+
138147
if err != nil {
139148
fmt.Fprint(os.Stderr, "\nInvalid configuration file", err)
140149
return
141150
}
142151

143-
fmt.Fprintf(os.Stderr, statusMsg, color.HiGreenString(clicfg.Services.Kafka.ClusterName), color.HiGreenString(currentNamespace), color.HiGreenString(secretName))
152+
fmt.Fprintf(os.Stderr, statusMsg, color.HiGreenString(kafkaInstance.Name), color.HiGreenString(currentNamespace), color.HiGreenString(secretName))
144153
if shouldContinue := showQuestion("Do you want to continue?"); shouldContinue == false {
145154
return
146155
}
@@ -150,7 +159,7 @@ func connectToCluster(connection *pkgConnection.Connection) {
150159
return
151160
}
152161
createSecret(credentials, currentNamespace, clientset)
153-
createCR(clicfg, clientset, currentNamespace)
162+
createCR(clientset, &kafkaInstance, currentNamespace)
154163
}
155164

156165
func fileExists(path string) bool {
@@ -241,8 +250,8 @@ func createSecret(credentials *managedservices.TokenResponse, currentNamespace s
241250
return secret
242251
}
243252

244-
func createCR(clicfg *config.Config, clientset *kubernetes.Clientset, namespace string) {
245-
crName := secretName + "-" + clicfg.Services.Kafka.ClusterName
253+
func createCR(clientset *kubernetes.Clientset, kafkaInstance *managedservices.KafkaRequest, namespace string) {
254+
crName := secretName + "-" + kafkaInstance.Name
246255
crInstance := &connection.ManagedKafkaConnection{
247256
ObjectMeta: metav1.ObjectMeta{
248257
Name: crName,
@@ -254,7 +263,7 @@ func createCR(clicfg *config.Config, clientset *kubernetes.Clientset, namespace
254263
},
255264
Spec: connection.ManagedKafkaConnectionSpec{
256265
BootstrapServer: connection.BootstrapServerSpec{
257-
Host: clicfg.Services.Kafka.ClusterHost,
266+
Host: kafkaInstance.BootstrapServerHost,
258267
},
259268
Credentials: connection.CredentialsSpec{
260269
Kind: connection.ClientCredentials,
@@ -313,7 +322,7 @@ func useKafka(cliconfig *config.Config, connection *pkgConnection.Connection) *c
313322
index, _, err := prompt.Run()
314323
if err == nil {
315324
selectedKafka := response.Items[index]
316-
var kafkaConfig config.KafkaConfig = config.KafkaConfig{ClusterID: selectedKafka.Id, ClusterName: selectedKafka.Name, ClusterHost: selectedKafka.BootstrapServerHost}
325+
var kafkaConfig config.KafkaConfig = config.KafkaConfig{ClusterID: selectedKafka.Id}
317326
cliconfig.Services.SetKafka(&kafkaConfig)
318327

319328
return cliconfig

pkg/cmd/kafka/create/create.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,6 @@ func runCreate(opts *options) error {
9696
}
9797

9898
kafkaCfg := &config.KafkaConfig{
99-
ClusterName: response.Name,
100-
ClusterHost: response.BootstrapServerHost,
10199
ClusterID: response.Id,
102100
}
103101

pkg/cmd/kafka/use/use.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ func runUse(opts *options) error {
6464
// build Kafka config object from the response
6565
var kafkaConfig config.KafkaConfig = config.KafkaConfig{
6666
ClusterID: res.Id,
67-
ClusterName: res.Name,
68-
ClusterHost: res.BootstrapServerHost,
6967
}
7068

7169
cfg.Services.SetKafka(&kafkaConfig)

pkg/cmd/serviceaccount/create/create.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,18 @@ import (
1616
// Templates
1717
var (
1818
templateProperties = heredoc.Doc(`
19-
## Credentials for Kafka cluster: '%v'
2019
## Generated by rhoas cli
2120
kafka_user=%v
2221
kafka_password=%v
2322
`)
2423

2524
templateEnv = heredoc.Doc(`
26-
## Credentials for Kafka cluster: '%v'
2725
## Generated by rhoas cli
2826
KAFKA_USER=%v
2927
KAFKA_PASSWORD=%v
3028
`)
3129

3230
templateKafkaPlain = heredoc.Doc(`
33-
## Credentials for Kafka cluster: '%v'
3431
## Generated by rhoas cli
3532
kafka.sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="%v" password="%v";
3633
kafka.sasl.mechanism=PLAIN
@@ -149,7 +146,7 @@ func runCreate(opts *options) error {
149146
}
150147

151148
fmt.Fprintf(os.Stderr, "Writing credentials to %v \n", fileName)
152-
fileContent := fmt.Sprintf(fileFormat, cfg.Services.Kafka.ClusterName, response.ClientID, response.ClientSecret)
149+
fileContent := fmt.Sprintf(fileFormat, response.ClientID, response.ClientSecret)
153150

154151
dataToWrite := []byte(fileContent)
155152

pkg/config/config.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ type ServiceConfigMap struct {
3232

3333
// KafkaConfig is the config for the managed Kafka service
3434
type KafkaConfig struct {
35-
ClusterHost string `json:"clusterHost"`
36-
ClusterID string `json:"clusterId"`
37-
ClusterName string `json:"clusterName"`
35+
ClusterID string `json:"clusterId"`
3836
}
3937

4038
func (c *Config) SetAccessToken(accessToken string) {
@@ -77,9 +75,7 @@ func (s *ServiceConfigMap) SetKafka(k *KafkaConfig) {
7775
// Remove the current Kafka cluster from the config
7876
func (s *ServiceConfigMap) RemoveKafka() {
7977
s.Kafka = &KafkaConfig{
80-
ClusterID: "",
81-
ClusterHost: "",
82-
ClusterName: "",
78+
ClusterID: "",
8379
}
8480
}
8581

pkg/sdk/kafka/topics/topics.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package topics
22

33
import (
4+
"context"
45
"fmt"
56
"net"
67
"os"
@@ -31,15 +32,28 @@ func brokerConnect() (broker *kafka.Conn, ctl *kafka.Conn) {
3132
fmt.Fprint(os.Stderr, err)
3233
}
3334

34-
if cfg.Services.Kafka.ClusterHost == "" {
35+
if cfg.Services.Kafka.ClusterID == "" {
3536
fmt.Fprint(os.Stderr, "No Kafka selected. Run rhoas kafka use")
3637
panic("Missing config")
3738
}
39+
40+
connection, err := cfg.Connection()
41+
if err != nil {
42+
fmt.Fprintf(os.Stderr, "Could not create connection: %v\n", err)
43+
}
44+
45+
managedservices := connection.NewMASClient()
46+
kafkaInstance, _, err := managedservices.DefaultApi.GetKafkaById(context.TODO(), cfg.Services.Kafka.ClusterID)
47+
if err != nil {
48+
fmt.Fprintf(os.Stderr, "Could not get Kafka instance: %v\n", err)
49+
return
50+
}
51+
3852
var clusterURL string
39-
if strings.HasPrefix(cfg.Services.Kafka.ClusterHost, "localhost") {
40-
clusterURL = cfg.Services.Kafka.ClusterHost
53+
if strings.HasPrefix(kafkaInstance.BootstrapServerHost, "localhost") {
54+
clusterURL = kafkaInstance.BootstrapServerHost
4155
} else {
42-
clusterURL = cfg.Services.Kafka.ClusterHost + ":443"
56+
clusterURL = kafkaInstance.BootstrapServerHost + ":443"
4357
}
4458

4559
conn, err := dialer.Dial("tcp", clusterURL)

0 commit comments

Comments
 (0)