Skip to content

Commit

Permalink
Delete NCP created certificate in cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Yun-Tang Hsu <hsuy@vmware.com>
  • Loading branch information
yuntanghsu committed Jul 1, 2024
1 parent b7bf875 commit dd5b667
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
12 changes: 7 additions & 5 deletions pkg/nsx/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import (
mpsearch "github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/search"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/trust_management"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/trust_management/principal_identities"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/domains/security_policies"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sites/enforcement_points"
projects "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects"
infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects"
project_infra "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/infra/realized_state"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs"
nat "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/nat"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/nat"
vpc_sp "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/security_policies"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/subnets"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt/orgs/projects/vpcs/subnets/ip_pools"
Expand Down Expand Up @@ -74,7 +75,8 @@ type Client struct {
OrgRootClient nsx_policy.OrgRootClient
ProjectInfraClient projects.InfraClient
VPCClient projects.VpcsClient
IPBlockClient infra.IpBlocksClient
IPBlockClient project_infra.IpBlocksClient
CertificateClient infra.CertificatesClient
StaticRouteClient vpcs.StaticRoutesClient
NATRuleClient nat.NatRulesClient
VpcGroupClient vpcs.GroupsClient
Expand Down Expand Up @@ -152,7 +154,7 @@ func GetClient(cf *config.NSXOperatorConfig) *Client {
orgRootClient := nsx_policy.NewOrgRootClient(restConnector(cluster))
projectInfraClient := projects.NewInfraClient(restConnector(cluster))
vpcClient := projects.NewVpcsClient(restConnector(cluster))
ipBlockClient := infra.NewIpBlocksClient(restConnector(cluster))
ipBlockClient := project_infra.NewIpBlocksClient(restConnector(cluster))
staticRouteClient := vpcs.NewStaticRoutesClient(restConnector(cluster))
natRulesClient := nat.NewNatRulesClient(restConnector(cluster))
vpcGroupClient := vpcs.NewGroupsClient(restConnector(cluster))
Expand Down
3 changes: 3 additions & 0 deletions pkg/nsx/services/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const (
TagScopeNCPVIFProjectUID string = "ncp/vif_project_uid"
TagScopeNCPPod string = "ncp/pod"
TagScopeNCPVNETInterface string = "ncp/vnet_interface"
TagScopeNCPDefaultLBCert string = "ncp/lb_default_cert"
TagScopeNCPSecret string = "ncp/secret"
TagScopeVersion string = "nsx-op/version"
TagScopeCluster string = "nsx-op/cluster"
TagScopeNamespace string = "nsx-op/namespace"
Expand Down Expand Up @@ -152,6 +154,7 @@ var (
ResourceTypeChildGroup = "ChildGroup"
ResourceTypeChildSecurityPolicy = "ChildSecurityPolicy"
ResourceTypeChildResourceReference = "ChildResourceReference"
ResourceTypeTlsCertificate = "TlsCertificate"

// ResourceTypeClusterControlPlane is used by NSXServiceAccountController
ResourceTypeClusterControlPlane = "clustercontrolplane"
Expand Down
9 changes: 9 additions & 0 deletions pkg/nsx/services/vpc/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ func (vs *VPCStore) GetByKey(key string) *model.Vpc {
return nil
}

// CertStore is a store to query certificate
type CertStore struct {
common.ResourceStore
}

func (certStore *CertStore) Apply(i interface{}) error {
return nil
}

func (is *IPBlockStore) GetByIndex(index string, value string) *model.IpAddressBlock {
indexResults, err := is.ResourceStore.Indexer.ByIndex(index, value)
if err != nil || len(indexResults) == 0 {
Expand Down
57 changes: 55 additions & 2 deletions pkg/nsx/services/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,11 @@ func InitializeVPC(service common.Service) (*VPCService, error) {
VPCService.VPCNSNetworkConfigStore = VPCNsNetworkConfigStore{
VPCNSNetworkConfigMap: make(map[string]string),
}
//initialize vpc store and ip blocks store
// initialize vpc store and ip blocks store
go VPCService.InitializeResourceStore(&wg, fatalErrors, common.ResourceTypeVpc, nil, VPCService.VpcStore)
go VPCService.InitializeResourceStore(&wg, fatalErrors, common.ResourceTypeIPBlock, nil, VPCService.IpblockStore)

//initalize avi rule related store
// initialize avi rule related store
if enableAviAllowRule {
VPCService.RuleStore = &AviRuleStore{ResourceStore: common.ResourceStore{
Indexer: cache.NewIndexer(keyFuncAVI, nil),
Expand Down Expand Up @@ -259,6 +259,44 @@ func (s *VPCService) DeleteVPC(path string) error {
return nil
}

func (s *VPCService) ListCert() []model.TlsCertificate {
certStore := &CertStore{ResourceStore: common.ResourceStore{
Indexer: cache.NewIndexer(keyFunc, cache.Indexers{}),
BindingType: model.TlsCertificateBindingType(),
}}
query := fmt.Sprintf("%s:%s", common.ResourceType, common.ResourceTypeTlsCertificate)
count, searcherr := s.SearchResource(common.ResourceTypeTlsCertificate, query, certStore, nil)
if searcherr != nil {
log.Error(searcherr, "failed to query certificate", "query", query)
} else {
log.V(1).Info("query certificate", "count", count)
}
certs := certStore.List()
certsSet := []model.TlsCertificate{}
for _, cert := range certs {
certsSet = append(certsSet, *cert.(*model.TlsCertificate))
}
return certsSet
}

func (s *VPCService) DeleteCert(id string) error {
certClient := s.NSXClient.CertificateClient
if err := certClient.Delete(id); err