Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameterize shard name #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pkg/controller/shard_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{})
}
Expand All @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pkg/manager/sharding_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down