This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delete Certificate for an Envoy for a Pod that was Terminated (#1956)
- Loading branch information
Showing
11 changed files
with
234 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package catalog | ||
|
||
import ( | ||
"errors" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/openservicemesh/osm/pkg/announcements" | ||
"github.com/openservicemesh/osm/pkg/certificate" | ||
) | ||
|
||
var errEventNotHandled = errors.New("event not handled") | ||
|
||
// releaseCertificate is an Announcement handler, which on receiving a PodDeleted event | ||
// it releases the xDS certificate for the Envoy for that Pod. | ||
func (mc *MeshCatalog) releaseCertificate(ann announcements.Announcement) error { | ||
whatWeGot := ann.Type | ||
whatWeCanHandle := announcements.PodDeleted | ||
if whatWeCanHandle != whatWeGot { | ||
log.Error().Msgf("releaseCertificate function received an announcement with type %s; it can only handle %s", whatWeGot, whatWeCanHandle) | ||
return errEventNotHandled | ||
} | ||
|
||
if podUID, ok := ann.ReferencedObjectID.(types.UID); ok { | ||
if podIface, ok := mc.podUIDToCN.Load(podUID); ok { | ||
endpointCN := podIface.(certificate.CommonName) | ||
log.Warn().Msgf("Pod with UID %s found in Mesh Catalog; Releasing certificate %s", podUID, endpointCN) | ||
mc.certManager.ReleaseCertificate(endpointCN) | ||
} else { | ||
log.Warn().Msgf("Pod with UID %s not found in Mesh Catalog", podUID) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// updateRelatedProxies is an Announcement handler, which augments the handling of PodDeleted events | ||
// and leverages broadcastToAllProxies() to let all proxies know that something has changed. | ||
// TODO: The use of broadcastToAllProxies() needs to be deprecated in favor of more granular approach. | ||
func (mc *MeshCatalog) updateRelatedProxies(ann announcements.Announcement) error { | ||
whatWeGot := ann.Type | ||
whatWeCanHandle := announcements.PodDeleted | ||
if whatWeCanHandle != whatWeGot { | ||
log.Error().Msgf("updateRelatedProxies function received an announcement with type %s; it can only handle %s", whatWeGot, whatWeCanHandle) | ||
return errEventNotHandled | ||
} | ||
|
||
// TODO: the function below updates all proxies; understand what proxies need to be updated and update only these | ||
mc.broadcastToAllProxies(ann) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package catalog | ||
|
||
import ( | ||
"time" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
"github.com/google/uuid" | ||
"k8s.io/apimachinery/pkg/types" | ||
testclient "k8s.io/client-go/kubernetes/fake" | ||
|
||
"github.com/openservicemesh/osm/pkg/announcements" | ||
"github.com/openservicemesh/osm/pkg/certificate" | ||
"github.com/openservicemesh/osm/pkg/envoy" | ||
) | ||
|
||
var _ = Describe("Test Announcement Handlers", func() { | ||
var mc *MeshCatalog | ||
var podUID string | ||
var proxy *envoy.Proxy | ||
var envoyCN certificate.CommonName | ||
|
||
BeforeEach(func() { | ||
mc = NewFakeMeshCatalog(testclient.NewSimpleClientset()) | ||
podUID = uuid.New().String() | ||
|
||
envoyCN = "abcdefg" | ||
_, err := mc.certManager.IssueCertificate(envoyCN, 5*time.Second) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
proxy = envoy.NewProxy(envoyCN, nil) | ||
proxy.PodMetadata = &envoy.PodMetadata{ | ||
UID: podUID, | ||
} | ||
|
||
mc.RegisterProxy(proxy) | ||
}) | ||
|
||
Context("test releaseCertificate()", func() { | ||
It("deletes certificate when Pod is terminated", func() { | ||
// Ensure setup is correct | ||
{ | ||
certs, err := mc.certManager.ListCertificates() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(len(certs)).To(Equal(1)) | ||
} | ||
|
||
ann := announcements.Announcement{ | ||
Type: announcements.PodDeleted, | ||
ReferencedObjectID: types.UID(podUID), | ||
} | ||
err := mc.releaseCertificate(ann) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Ensure certificate was deleted | ||
{ | ||
certs, err := mc.certManager.ListCertificates() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(len(certs)).To(Equal(0)) | ||
} | ||
}) | ||
|
||
It("ignores events other than pod-deleted", func() { | ||
ann := announcements.Announcement{ | ||
Type: announcements.IngressAdded, | ||
} | ||
|
||
var connectedProxies []envoy.Proxy | ||
mc.connectedProxies.Range(func(key interface{}, value interface{}) bool { | ||
connectedProxy := value.(connectedProxy) | ||
connectedProxies = append(connectedProxies, *connectedProxy.proxy) | ||
return true | ||
}) | ||
|
||
Expect(len(connectedProxies)).To(Equal(1)) | ||
Expect(connectedProxies[0]).To(Equal(*proxy)) | ||
|
||
err := mc.releaseCertificate(ann) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
Context("test updateRelatedProxies()", func() { | ||
It("ignores events other than pod-deleted", func() { | ||
ann := announcements.Announcement{ | ||
Type: announcements.IngressAdded, | ||
} | ||
|
||
var connectedProxies []envoy.Proxy | ||
mc.connectedProxies.Range(func(key interface{}, value interface{}) bool { | ||
connectedProxy := value.(connectedProxy) | ||
connectedProxies = append(connectedProxies, *connectedProxy.proxy) | ||
return true | ||
}) | ||
|
||
Expect(len(connectedProxies)).To(Equal(1)) | ||
Expect(connectedProxies[0]).To(Equal(*proxy)) | ||
|
||
err := mc.updateRelatedProxies(ann) | ||
Expect(err).To(HaveOccurred()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters