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

process gtps as soon they are applied #223

Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 25 additions & 3 deletions admiral/pkg/clusters/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package clusters
import (
"context"
"errors"
"fmt"
"sync"
"time"

argo "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1"
"github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/v1"
v1 "github.com/istio-ecosystem/admiral/admiral/pkg/apis/admiral/v1"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/admiral"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/istio"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/secret"
log "github.com/sirupsen/logrus"
k8sAppsV1 "k8s.io/api/apps/v1"
k8s "k8s.io/client-go/kubernetes"
"sync"
"time"
)

type RemoteController struct {
Expand Down Expand Up @@ -180,14 +182,17 @@ func (dh *DependencyHandler) Deleted(obj *v1.Dependency) {

func (gtp *GlobalTrafficHandler) Added(obj *v1.GlobalTrafficPolicy) {
log.Infof(LogFormat, "Added", "globaltrafficpolicy", obj.Name, gtp.ClusterID, "received")
HandleEventForGlobalTrafficPolicy(admiral.Add, obj, gtp.RemoteRegistry, gtp.ClusterID)
}

func (gtp *GlobalTrafficHandler) Updated(obj *v1.GlobalTrafficPolicy) {
log.Infof(LogFormat, "Updated", "globaltrafficpolicy", obj.Name, gtp.ClusterID, "received")
HandleEventForGlobalTrafficPolicy(admiral.Update, obj, gtp.RemoteRegistry, gtp.ClusterID)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will an assignment of _ = HandleEventForGlobalTrafficPolicy(..) help to prevent warning from Github Actions?

}

func (gtp *GlobalTrafficHandler) Deleted(obj *v1.GlobalTrafficPolicy) {
log.Infof(LogFormat, "Deleted", "globaltrafficpolicy", obj.Name, gtp.ClusterID, "received")
HandleEventForGlobalTrafficPolicy(admiral.Delete, obj, gtp.RemoteRegistry, gtp.ClusterID)
}

func (pc *DeploymentHandler) Added(obj *k8sAppsV1.Deployment) {
Expand Down Expand Up @@ -243,3 +248,20 @@ func HandleEventForDeployment(event admiral.EventType, obj *k8sAppsV1.Deployment
// Use the same function as added deployment function to update and put new service entry in place to replace old one
modifyServiceEntryForNewServiceOrPod(event, env, globalIdentifier, remoteRegistry)
}

// HandleEventForGlobalTrafficPolicy processes all the events related to GTPs
func HandleEventForGlobalTrafficPolicy(event admiral.EventType, gtp *v1.GlobalTrafficPolicy, remoteRegistry *RemoteRegistry, clusterName string) error {

globalIdentifier := common.GetGtpIdentity(gtp)

if len(globalIdentifier) == 0 {
log.Infof(LogFormat, "Event", "globaltrafficpolicy", gtp.Name, clusterName, "Skipped as '"+common.GetWorkloadIdentifier()+" was not found', namespace="+gtp.Namespace)
return fmt.Errorf("missing %s on gtp named %s on cluster %s", common.GetWorkloadIdentifier(), gtp.Name, clusterName)
}

env := common.GetGtpEnv(gtp)

// Use the same function as added deployment function to update and put new service entry in place to replace old one
modifyServiceEntryForNewServiceOrPod(event, env, globalIdentifier, remoteRegistry)
return nil
}
57 changes: 56 additions & 1 deletion admiral/pkg/clusters/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
admiralFake "github.com/istio-ecosystem/admiral/admiral/pkg/client/clientset/versioned/fake"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/admiral"
"github.com/istio-ecosystem/admiral/admiral/pkg/controller/common"
"github.com/stretchr/testify/assert"
v12 "k8s.io/api/apps/v1"
v13 "k8s.io/api/core/v1"
time2 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -197,10 +198,64 @@ func TestRolloutHandler(t *testing.T) {
gtpCache.identityCache = make(map[string]*v1.GlobalTrafficPolicy)
gtpCache.mutex = &sync.Mutex{}
handler.RemoteRegistry.AdmiralCache.GlobalTrafficCache = gtpCache

handler.Added(c.addedRolout)
handler.Deleted(c.addedRolout)
handler.Updated(c.addedRolout)
})
}
}

func TestHandleEventForGlobalTrafficPolicy(t *testing.T) {
event := admiral.EventType("Add")
p := common.AdmiralParams{
KubeconfigPath: "testdata/fake.config",
}
registry, _ := InitAdmiral(context.Background(), p)

testcases := []struct {
name string
gtp *v1.GlobalTrafficPolicy
doesError bool
}{
{
name: "missing identity label in GTP should result in error being returned by the handler",
gtp: &v1.GlobalTrafficPolicy{
ObjectMeta: time2.ObjectMeta{
Name: "testgtp",
Annotations: map[string]string{"admiral.io/env": "testenv"},
},
},
doesError: true,
},
{
name: "empty identity label in GTP should result in error being returned by the handler",
gtp: &v1.GlobalTrafficPolicy{
ObjectMeta: time2.ObjectMeta{
Name: "testgtp",
Labels: map[string]string{"identity": ""},
Annotations: map[string]string{"admiral.io/env": "testenv"},
},
},
doesError: true,
},
{
name: "valid GTP config which is expected to pass",
gtp: &v1.GlobalTrafficPolicy{
ObjectMeta: time2.ObjectMeta{
Name: "testgtp",
Labels: map[string]string{"identity": "testapp"},
Annotations: map[string]string{"admiral.io/env": "testenv"},
},
},
doesError: false,
},
}

for _, c := range testcases {
t.Run(c.name, func(t *testing.T) {
err := HandleEventForGlobalTrafficPolicy(event, c.gtp, registry, "testcluster")
assert.Equal(t, err != nil, c.doesError)
})
}

}