diff --git a/pkg/controller/shard_handler.go b/pkg/controller/shard_handler.go index 923b4b6..33dc924 100644 --- a/pkg/controller/shard_handler.go +++ b/pkg/controller/shard_handler.go @@ -3,10 +3,13 @@ package controller import ( "context" "fmt" + "strings" typeV1 "github.com/istio-ecosystem/admiral-api/pkg/apis/admiral/v1" "github.com/istio-ecosystem/admiral-sharding-manager/pkg/model" "github.com/istio-ecosystem/admiral-sharding-manager/pkg/registry" + "github.com/sirupsen/logrus" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -44,6 +47,14 @@ func (sh *shardHandler) Create( clusterConfiguration []registry.ClusterConfig, shardName string, operatorIdentity string) (*typeV1.Shard, error) { + + shardName = strings.ToLower(shardName) + _, err := sh.clients.AdmiralClient.Shards(sh.params.ShardNamespace).Get(ctx, shardName, metav1.GetOptions{}) + logrus.Warnf("error getting shard: %v", err) + if errors.IsAlreadyExists(err) { + logrus.Warnf("shard=%s already exists, updating instead...", shardName) + return sh.Update(ctx, clusterConfiguration, shardName, operatorIdentity) + } shardToCreate := buildShardResource(clusterConfiguration, sh.params, shardName, operatorIdentity) return sh.clients.AdmiralClient.Shards(sh.params.ShardNamespace).Create(ctx, shardToCreate, metav1.CreateOptions{}) } @@ -53,6 +64,7 @@ func (sh *shardHandler) Update( clusterConfiguration []registry.ClusterConfig, shardName string, operatorIdentity string) (*typeV1.Shard, error) { + shardName = strings.ToLower(shardName) var updatedShard *typeV1.Shard existingShard, err := sh.clients.AdmiralClient.Shards(sh.params.ShardNamespace).Get(ctx, shardName, metav1.GetOptions{}) shardToUpdate := buildShardResource(clusterConfiguration, sh.params, shardName, operatorIdentity) @@ -78,7 +90,11 @@ func (sh *shardHandler) Delete(ctx context.Context, shard *typeV1.Shard) error { return err } -func buildShardResource(clusterConfigs []registry.ClusterConfig, smParam *model.ShardingManagerParams, shardName string, operatorIdentity string) *typeV1.Shard { +func buildShardResource( + clusterConfigs []registry.ClusterConfig, + smParam *model.ShardingManagerParams, + shardName string, + operatorIdentity string) *typeV1.Shard { var ( clusters []typeV1.ClusterShards labels = make(map[string]string) diff --git a/pkg/manager/sharding_manager.go b/pkg/manager/sharding_manager.go index 4291293..8f75eb0 100644 --- a/pkg/manager/sharding_manager.go +++ b/pkg/manager/sharding_manager.go @@ -48,12 +48,12 @@ func (sm *shardingManager) Start(ctx context.Context) error { } func (sm *shardingManager) pushShardConfiguration(ctx context.Context, config model.ShardingMangerCache) error { - _, err := sm.shardHandler.Create(ctx, config.ClusterCache, "identity", "operatorIdentity") + _, err := sm.shardHandler.Create(ctx, config.ClusterCache, sm.identity+"-"+"operatorIdentity", "operatorIdentity") if err != nil { logrus.Warnf("error creating shard: %v", err) if errors.IsAlreadyExists(err) { logrus.Info("shard already exists, updating it...") - _, err = sm.shardHandler.Update(ctx, config.ClusterCache, "identity", "operatorIdentity") + _, err = sm.shardHandler.Update(ctx, config.ClusterCache, sm.identity+"-"+"operatorIdentity", "operatorIdentity") return err } }