-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Pierangelo Di Pilato <pierangelodipilato@gmail.com>
- Loading branch information
Showing
29 changed files
with
619 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2020 The Knative Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package broker | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/Shopify/sarama" | ||
"go.uber.org/zap" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// PathFromObject returns an HTTP request path given a generic object. | ||
func PathFromObject(obj metav1.Object) string { | ||
return Path(obj.GetNamespace(), obj.GetName()) | ||
} | ||
|
||
// Path returns an HTTP request path given namespace and name of an object. | ||
func Path(namespace, name string) string { | ||
return fmt.Sprintf("/%s/%s", namespace, name) | ||
} | ||
|
||
// Topic returns a topic name given a topic prefix and a generic object. | ||
func Topic(prefix string, obj metav1.Object) string { | ||
return fmt.Sprintf("%s%s-%s", prefix, obj.GetNamespace(), obj.GetName()) | ||
} | ||
|
||
// CreateTopic creates a topic with name 'topic' following the TopicConfig configuration passed as parameter. | ||
// | ||
// It returns the topic name or an error. | ||
// | ||
// If the topic already exists, it will return no errors. | ||
func CreateTopic(admin sarama.ClusterAdmin, logger *zap.Logger, topic string, config *TopicConfig) (string, error) { | ||
|
||
topicDetail := &sarama.TopicDetail{ | ||
NumPartitions: config.TopicDetail.NumPartitions, | ||
ReplicationFactor: config.TopicDetail.ReplicationFactor, | ||
} | ||
|
||
logger.Debug("create topic", | ||
zap.String("topic", topic), | ||
zap.Int16("replicationFactor", topicDetail.ReplicationFactor), | ||
zap.Int32("numPartitions", topicDetail.NumPartitions), | ||
) | ||
|
||
createTopicError := admin.CreateTopic(topic, topicDetail, false) | ||
if err, ok := createTopicError.(*sarama.TopicError); ok && err.Err == sarama.ErrTopicAlreadyExists { | ||
return topic, nil | ||
} | ||
|
||
return topic, createTopicError | ||
} | ||
|
||
// NewClusterAdminFunc creates new sarama.ClusterAdmin. | ||
type NewClusterAdminFunc func(addrs []string, config *sarama.Config) (sarama.ClusterAdmin, error) | ||
|
||
// GetClusterAdmin create a new sarama.ClusterAdmin. | ||
// | ||
// The caller is responsible for closing the sarama.ClusterAdmin. | ||
func GetClusterAdmin(adminFunc NewClusterAdminFunc, bootstrapServers []string) (sarama.ClusterAdmin, error) { | ||
config := sarama.NewConfig() | ||
config.Version = sarama.MaxVersion | ||
|
||
kafkaClusterAdmin, err := adminFunc(bootstrapServers, config) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create cluster admin: %w", err) | ||
} | ||
|
||
return kafkaClusterAdmin, nil | ||
} |
47 changes: 47 additions & 0 deletions
47
control-plane/pkg/reconciler/base/broker/broker_base_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2020 The Knative Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package broker | ||
|
||
import "testing" | ||
|
||
func TestPath(t *testing.T) { | ||
type args struct { | ||
namespace string | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "namespace/name", | ||
args: args{ | ||
namespace: "broker-namespace", | ||
name: "broker-name", | ||
}, | ||
want: "/broker-namespace/broker-name", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Path(tt.args.namespace, tt.args.name); got != tt.want { | ||
t.Errorf("Path() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.