From cf67d610ddf4a7af8603a1f367ba63f24e8ddd09 Mon Sep 17 00:00:00 2001 From: Felix Kunde Date: Tue, 9 Feb 2021 11:00:52 +0100 Subject: [PATCH] intermediate garbage --- pkg/controller/controller.go | 3 +- pkg/controller/util.go | 4 +- pkg/teams/postgres_team.go | 19 ++++++++-- pkg/teams/postgres_team_test.go | 66 +++++++++++++++++++-------------- 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 7aca23346..667d9ba95 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -332,8 +332,7 @@ func (c *Controller) initController() { if c.opConfig.EnablePostgresTeamCRD { c.loadPostgresTeams() } else { - teamMap := make(teams.PostgresTeamMap, 0) - c.pgTeamMap = &teamMap + c.pgTeamMap.Reset() } if c.opConfig.DebugLogging { diff --git a/pkg/controller/util.go b/pkg/controller/util.go index ac0a32e25..ed770935c 100644 --- a/pkg/controller/util.go +++ b/pkg/controller/util.go @@ -15,7 +15,6 @@ import ( acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" "github.com/zalando/postgres-operator/pkg/cluster" "github.com/zalando/postgres-operator/pkg/spec" - "github.com/zalando/postgres-operator/pkg/teams" "github.com/zalando/postgres-operator/pkg/util" "github.com/zalando/postgres-operator/pkg/util/config" "github.com/zalando/postgres-operator/pkg/util/k8sutil" @@ -396,8 +395,7 @@ func (c *Controller) getInfrastructureRole( func (c *Controller) loadPostgresTeams() { // reset team map - teamMap := make(teams.PostgresTeamMap, 0) - c.pgTeamMap = &teamMap + c.pgTeamMap.Reset() pgTeams, err := c.KubeClient.PostgresTeamsGetter.PostgresTeams(c.opConfig.WatchedNamespace).List(context.TODO(), metav1.ListOptions{}) if err != nil { diff --git a/pkg/teams/postgres_team.go b/pkg/teams/postgres_team.go index 7fb725765..0f835862b 100644 --- a/pkg/teams/postgres_team.go +++ b/pkg/teams/postgres_team.go @@ -6,9 +6,11 @@ import ( ) // PostgresTeamMap is the operator's internal representation of all PostgresTeam CRDs -type PostgresTeamMap map[string]postgresTeamMembership +type PostgresTeamMap map[string]*PostgresTeamMembership -type postgresTeamMembership struct { +var emptyTeamMap = make(PostgresTeamMap, 0) + +type PostgresTeamMembership struct { AdditionalSuperuserTeams []string AdditionalTeams []string AdditionalMembers []string @@ -94,6 +96,10 @@ func (ptm *PostgresTeamMap) GetAdditionalSuperuserTeams(team string, transitive // Load function to import data from PostgresTeam CRD func (ptm *PostgresTeamMap) Load(pgTeams *acidv1.PostgresTeamList) { + if ptm == nil { + ptm = &emptyTeamMap + } + superuserTeamSet := teamHashSet{} teamSet := teamHashSet{} teamMemberSet := teamHashSet{} @@ -109,10 +115,17 @@ func (ptm *PostgresTeamMap) Load(pgTeams *acidv1.PostgresTeamList) { fetchTeams(&teamIDs, teamMemberSet) for teamID := range teamIDs { - (*ptm)[teamID] = postgresTeamMembership{ + (*ptm)[teamID] = &PostgresTeamMembership{ AdditionalSuperuserTeams: util.CoalesceStrArr(superuserTeamSet.toMap()[teamID], []string{}), AdditionalTeams: util.CoalesceStrArr(teamSet.toMap()[teamID], []string{}), AdditionalMembers: util.CoalesceStrArr(teamMemberSet.toMap()[teamID], []string{}), } } } + +// Reset a PostgresTeamMap +func (ptm *PostgresTeamMap) Reset() { + if ptm != nil { + *ptm = emptyTeamMap + } +} diff --git a/pkg/teams/postgres_team_test.go b/pkg/teams/postgres_team_test.go index f8c3a21d8..9f7340744 100644 --- a/pkg/teams/postgres_team_test.go +++ b/pkg/teams/postgres_team_test.go @@ -3,6 +3,7 @@ package teams import ( "testing" + "github.com/stretchr/testify/assert" acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1" "github.com/zalando/postgres-operator/pkg/util" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -46,9 +47,36 @@ var ( }, }, } + pgTeamMap = PostgresTeamMap{ + "teamA": { + AdditionalSuperuserTeams: []string{"teamB", "team24x7"}, + AdditionalTeams: []string{"teamC"}, + AdditionalMembers: []string{}, + }, + "teamB": { + AdditionalSuperuserTeams: []string{"teamA", "teamC", "team24x7"}, + AdditionalTeams: []string{}, + AdditionalMembers: []string{"drno"}, + }, + "teamC": { + AdditionalSuperuserTeams: []string{"team24x7"}, + AdditionalTeams: []string{"teamA", "teamB", "acid"}, + AdditionalMembers: []string{}, + }, + "team24x7": { + AdditionalSuperuserTeams: []string{}, + AdditionalTeams: []string{}, + AdditionalMembers: []string{"optimusprime"}, + }, + "acid": { + AdditionalSuperuserTeams: []string{}, + AdditionalTeams: []string{}, + AdditionalMembers: []string{"batman"}, + }, + } ) -// PostgresTeamMap is the operator's internal representation of all PostgresTeam CRDs +// TestLoadingPostgresTeamCRD PostgresTeamMap is the operator's internal representation of all PostgresTeam CRDs func TestLoadingPostgresTeamCRD(t *testing.T) { tests := []struct { name string @@ -59,33 +87,7 @@ func TestLoadingPostgresTeamCRD(t *testing.T) { { "Check that CRD is imported correctly into the internal format", pgTeamList, - PostgresTeamMap{ - "teamA": { - AdditionalSuperuserTeams: []string{"teamB", "team24x7"}, - AdditionalTeams: []string{"teamC"}, - AdditionalMembers: []string{}, - }, - "teamB": { - AdditionalSuperuserTeams: []string{"teamA", "teamC", "team24x7"}, - AdditionalTeams: []string{}, - AdditionalMembers: []string{"drno"}, - }, - "teamC": { - AdditionalSuperuserTeams: []string{"team24x7"}, - AdditionalTeams: []string{"teamA", "teamB", "acid"}, - AdditionalMembers: []string{}, - }, - "team24x7": { - AdditionalSuperuserTeams: []string{}, - AdditionalTeams: []string{}, - AdditionalMembers: []string{"optimusprime"}, - }, - "acid": { - AdditionalSuperuserTeams: []string{}, - AdditionalTeams: []string{}, - AdditionalMembers: []string{"batman"}, - }, - }, + pgTeamMap, "Mismatch between PostgresTeam CRD and internal map", }, } @@ -107,6 +109,14 @@ func TestLoadingPostgresTeamCRD(t *testing.T) { } } +// TestLoadingPostgresTeamCRD PostgresTeamMap is the operator's internal representation of all PostgresTeam CRDs +func TestResetPostgresTeamCRD(t *testing.T) { + teamMap := &pgTeamMap + teamMap.Reset() + + assert.Equal(t, teamMap, &PostgresTeamMap{}) +} + // TestGetAdditionalTeams if returns teams with and without transitive dependencies func TestGetAdditionalTeams(t *testing.T) { tests := []struct {