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

KSPACE-20: Drop the distinction between host & member ToolchainClusters #359

Merged
merged 31 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c9f5793
KSPACE-20: Drop the distinction between host & member ToolchainClusters
fbm3307 Jan 30, 2024
97fb46c
Merge branch 'master' into ks20drptcrtype
fbm3307 Jan 31, 2024
9d4840e
Unit Test cases
fbm3307 Feb 5, 2024
561b13e
Some extra changes to Unit code
fbm3307 Feb 5, 2024
dddbdb1
Test cases miss
fbm3307 Feb 5, 2024
5c8a905
code coverage
fbm3307 Feb 6, 2024
3d7d055
Removing comments
fbm3307 Feb 6, 2024
b99c6fe
test coverage
fbm3307 Feb 6, 2024
6d8b8f0
Merge branch 'master' into ks20drptcrtype
fbm3307 Feb 8, 2024
ef55b59
Dropping the type name in func
fbm3307 Feb 12, 2024
8aa8eb8
Changing the logic for adding clusterrolelabel
fbm3307 Feb 14, 2024
d160665
Merge branch 'master' into ks20drptcrtype
fbm3307 Feb 19, 2024
acec724
Merge branch 'master' into ks20drptcrtype
fbm3307 Feb 26, 2024
41be3a9
Merge branch 'master' into ks20drptcrtype
fbm3307 Feb 28, 2024
abf81ab
Review comments
fbm3307 Mar 4, 2024
8505155
Test coverage
fbm3307 Mar 5, 2024
76ca387
Merge branch 'master' into ks20drptcrtype
fbm3307 Mar 5, 2024
b61c605
Adding the failure if no operator namespace set
fbm3307 Mar 6, 2024
ccc7739
Review comments-2
fbm3307 Mar 7, 2024
f637615
Unit test
fbm3307 Mar 7, 2024
f1d3446
Merge branch 'master' into ks20drptcrtype
mfrancisc Mar 7, 2024
450b432
Review Comments -3
fbm3307 Mar 8, 2024
b930930
.go lint
fbm3307 Mar 8, 2024
fefba7b
unit test
fbm3307 Mar 8, 2024
e34d97c
fixing Unit test cases
fbm3307 Mar 8, 2024
5e59741
Adding the gettoolchainclusters test in parallel
fbm3307 Mar 11, 2024
406939c
review comments4
fbm3307 Mar 11, 2024
4a1c1c5
Reviewcoments 5
fbm3307 Mar 11, 2024
e0997be
go-lint fix
fbm3307 Mar 11, 2024
069072d
Updating the name to tcns to avoid confusion
fbm3307 Mar 12, 2024
99a2215
Merge branch 'master' into ks20drptcrtype
MatousJobanek Mar 13, 2024
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
6 changes: 2 additions & 4 deletions controllers/toolchaincluster/healthchecker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func TestClusterHealthChecks(t *testing.T) {
unstable, sec := newToolchainCluster("unstable", "http://unstable.com", withStatus(healthy()))
notFound, _ := newToolchainCluster("not-found", "http://not-found.com", withStatus(notOffline(), unhealthy()))
stable, _ := newToolchainCluster("stable", "http://cluster.com", withStatus(offline()))

cl := test.NewFakeClient(t, unstable, notFound, stable, sec)
resetCache := setupCachedClusters(t, cl, unstable, notFound, stable)
defer resetCache()
Expand All @@ -72,7 +71,6 @@ func TestClusterHealthChecks(t *testing.T) {

t.Run("if no zones nor region is retrieved, then keep the current", func(t *testing.T) {
stable, sec := newToolchainCluster("stable", "http://cluster.com", withStatus(offline()))

cl := test.NewFakeClient(t, stable, sec)
resetCache := setupCachedClusters(t, cl, stable)
defer resetCache()
Expand All @@ -98,7 +96,7 @@ func TestClusterHealthChecks(t *testing.T) {
}

func setupCachedClusters(t *testing.T, cl *test.FakeClient, clusters ...*toolchainv1alpha1.ToolchainCluster) func() {
service := cluster.NewToolchainClusterServiceWithClient(cl, logf.Log, "test-namespace", 0, func(config *rest.Config, options client.Options) (client.Client, error) {
service := cluster.NewToolchainClusterServiceWithClient(cl, logf.Log, test.MemberOperatorNs, 0, func(config *rest.Config, options client.Options) (client.Client, error) {
// make sure that insecure is false to make Gock mocking working properly
config.Insecure = false
return client.New(config, options)
Expand All @@ -124,7 +122,7 @@ func withStatus(conditions ...toolchainv1alpha1.ToolchainClusterCondition) toolc
}

func newToolchainCluster(name, apiEndpoint string, status toolchainv1alpha1.ToolchainClusterStatus) (*toolchainv1alpha1.ToolchainCluster, *corev1.Secret) {
toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, "secret", apiEndpoint, status, map[string]string{})
toolchainCluster, secret := test.NewToolchainClusterWithEndpoint(name, "test-namespace", "secret", apiEndpoint, status, map[string]string{"namespace": "test-namespace"})
fbm3307 marked this conversation as resolved.
Show resolved Hide resolved
return toolchainCluster, secret
}

Expand Down
28 changes: 0 additions & 28 deletions controllers/toolchaincluster/toolchaincluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,5 @@ func (r *Reconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.
return reconcile.Result{}, err
}

// add toolchaincluster role label if not present
reqLogger.Info("adding cluster role label based on type")
if err := r.addToolchainClusterRoleLabelFromType(ctx, toolchainCluster); err != nil {
return reconcile.Result{}, err
}

return reconcile.Result{}, r.clusterCacheService.AddOrUpdateToolchainCluster(toolchainCluster)
}

func (r *Reconciler) addToolchainClusterRoleLabelFromType(ctx context.Context, toolchainCluster *toolchainv1alpha1.ToolchainCluster) error {
logger := log.FromContext(ctx)

if clusterType, found := toolchainCluster.Labels[cluster.LabelType]; !found {
logger.Info("cluster `type` label not found, unable to add toolchain cluster role label from type")
return nil
} else if clusterType != string(cluster.Member) {
logger.Info("cluster `type` is not member, skipping cluster role label setting")
return nil
}
clusterRoleLabel := cluster.RoleLabel(cluster.Tenant)
if _, exists := toolchainCluster.Labels[clusterRoleLabel]; !exists {
logger.Info("setting cluster role label for toolchaincluster", clusterRoleLabel, toolchainCluster.Name)
// We use only the label key, the value can remain empty.
toolchainCluster.Labels[clusterRoleLabel] = ""
if err := r.client.Update(ctx, toolchainCluster); err != nil {
fbm3307 marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
return nil
}
34 changes: 11 additions & 23 deletions pkg/cluster/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ type Config struct {
// APIEndpoint is the API endpoint of the corresponding ToolchainCluster. This can be a hostname,
// hostname:port, IP or IP:port.
APIEndpoint string
// Type is a type of the cluster (either host or member)
Type Type
// OperatorNamespace is a name of a namespace (in the cluster) the operator is running in
OperatorNamespace string
// OwnerClusterName keeps the name of the cluster the ToolchainCluster resource is created in
Expand Down Expand Up @@ -81,23 +79,21 @@ var Ready Condition = func(cluster *CachedToolchainCluster) bool {
return IsReady(cluster.ClusterStatus)
}

func (c *toolchainClusterClients) getCachedToolchainClustersByType(clusterType Type, conditions ...Condition) []*CachedToolchainCluster {
func (c *toolchainClusterClients) getCachedToolchainClusters(conditions ...Condition) []*CachedToolchainCluster {
c.RLock()
defer c.RUnlock()
return Filter(clusterType, c.clusters, conditions...)
return Filter(c.clusters, conditions...)
}
func Filter(clusterType Type, clusters map[string]*CachedToolchainCluster, conditions ...Condition) []*CachedToolchainCluster {
func Filter(clusters map[string]*CachedToolchainCluster, conditions ...Condition) []*CachedToolchainCluster {
fbm3307 marked this conversation as resolved.
Show resolved Hide resolved
filteredClusters := make([]*CachedToolchainCluster, 0, len(clusters))
clusters:
for _, cluster := range clusters {
if cluster.Type == clusterType {
for _, match := range conditions {
if !match(cluster) {
continue clusters
}
for _, match := range conditions {
if !match(cluster) {
continue clusters
}
filteredClusters = append(filteredClusters, cluster)
}
filteredClusters = append(filteredClusters, cluster)
}
return filteredClusters
}
Expand All @@ -117,12 +113,12 @@ var HostCluster GetHostClusterFunc = GetHostCluster
// GetHostCluster returns the kube client for the host cluster from the cache of the clusters
// and info if such a client exists
func GetHostCluster() (*CachedToolchainCluster, bool) {
fbm3307 marked this conversation as resolved.
Show resolved Hide resolved
clusters := clusterCache.getCachedToolchainClustersByType(Host)
clusters := clusterCache.getCachedToolchainClusters()
if len(clusters) == 0 {
if clusterCache.refreshCache != nil {
clusterCache.refreshCache()
}
clusters = clusterCache.getCachedToolchainClustersByType(Host)
clusters = clusterCache.getCachedToolchainClusters()
if len(clusters) == 0 {
return nil, false
}
Expand All @@ -138,24 +134,16 @@ var MemberClusters GetMemberClustersFunc = GetMemberClusters

// GetMemberClusters returns the kube clients for the host clusters from the cache of the clusters
func GetMemberClusters(conditions ...Condition) []*CachedToolchainCluster {
clusters := clusterCache.getCachedToolchainClustersByType(Member, conditions...)
clusters := clusterCache.getCachedToolchainClusters(conditions...)
if len(clusters) == 0 {
if clusterCache.refreshCache != nil {
clusterCache.refreshCache()
}
clusters = clusterCache.getCachedToolchainClustersByType(Member, conditions...)
clusters = clusterCache.getCachedToolchainClusters(conditions...)
}
return clusters
}

// Type is a cluster type (either host or member)
type Type string

const (
Member Type = "member"
Host Type = "host"
)

// Role defines the role of the cluster.
// Each type of cluster can have multiple roles (tenant for specific APIs, user workloads, others ... )
type Role string
Expand Down
Loading
Loading