Skip to content

Commit

Permalink
** Added Specific endpoint names that able to be used/customized.
Browse files Browse the repository at this point in the history
** Added validation for the GCP Service Endpoints.
  • Loading branch information
barbacbd committed Jan 30, 2025
1 parent 5e9556c commit f42c756
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
17 changes: 17 additions & 0 deletions pkg/types/gcp/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ import (
"github.com/openshift/installer/pkg/types/dns"
)

const (
// CloudResourceManagerServiceName is the name and internal key for the cloud resource manager API endpoint.
CloudResourceManagerServiceName = "cloudresourcemanager"
// ComputeServiceName is the name and internal key for the compute API endpoint.
ComputeServiceName = "compute"
// DNSServiceName is the name and internal key for the DNS API endpoint.
DNSServiceName = "dns"
// FileServiceName is the name and internal key for the file API endpoint.
FileServiceName = "file"
// IAMServiceName is the name and internal key for the IAM API endpoint.
IAMServiceName = "iam"
// ServiceUsageServiceName is the name and internal key for the service usage API endpoint.
ServiceUsageServiceName = "serviceusage"
// StorageServiceName is the name and internal key for the storage API endpoint.
StorageServiceName = "storage"
)

// Platform stores all the global configuration that all machinesets
// use.
type Platform struct {
Expand Down
18 changes: 16 additions & 2 deletions pkg/types/gcp/validation/platform.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"sort"

"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/openshift/installer/pkg/types"
Expand Down Expand Up @@ -77,6 +78,16 @@ var (

// userLabelKeyPrefixRegex is for verifying that the label key does not contain restricted prefixes.
userLabelKeyPrefixRegex = regexp.MustCompile(`^(?i)(kubernetes\-io|openshift\-io)`)

supportedEndpointNames = sets.New(
gcp.CloudResourceManagerServiceName,
gcp.ComputeServiceName,
gcp.DNSServiceName,
gcp.FileServiceName,
gcp.IAMServiceName,
gcp.ServiceUsageServiceName,
gcp.StorageServiceName,
)
)

const (
Expand Down Expand Up @@ -169,8 +180,11 @@ func validateServiceEndpoints(endpoints []gcp.ServiceEndpoint, fldPath *field.Pa
tracker := map[string]int{}
for idx, e := range endpoints {
fldp := fldPath.Index(idx)
if eidx, ok := tracker[e.Name]; ok {
allErrs = append(allErrs, field.Invalid(fldp.Child("name"), e.Name, fmt.Sprintf("duplicate service endpoint not allowed for %s, service endpoint already defined at %s", e.Name, fldPath.Index(eidx))))
if !supportedEndpointNames.Has(e.Name) {
allErrs = append(allErrs, field.NotSupported(fldp.Child("name"), e.Name, sets.List(supportedEndpointNames)))
}
if _, ok := tracker[e.Name]; ok {
allErrs = append(allErrs, field.Duplicate(fldp.Child("name"), e.Name))
} else {
tracker[e.Name] = idx
}
Expand Down
82 changes: 82 additions & 0 deletions pkg/types/gcp/validation/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,88 @@ func TestValidatePlatform(t *testing.T) {
credentialsMode: types.MintCredentialsMode,
valid: false,
},
{
name: "invalid gcp endpoint blank name",
platform: &gcp.Platform{
Region: "us-east1",
ServiceEndpoints: []gcp.ServiceEndpoint{
{
Name: "",
URL: "https://my-custom-endpoint.example.com/copmute/v1/",
},
},
},
valid: false,
},
{
name: "invalid gcp endpoint invalid name",
platform: &gcp.Platform{
Region: "us-east1",
ServiceEndpoints: []gcp.ServiceEndpoint{
{
Name: "badname",
URL: "https://my-custom-endpoint.example.com/copmute/v1/",
},
},
},
valid: false,
},
{
name: "invalid gcp endpoint duplicate name",
platform: &gcp.Platform{
Region: "us-east1",
ServiceEndpoints: []gcp.ServiceEndpoint{
{
Name: "compute",
URL: "https://my-custom-endpoint.example.com/compute/v1/",
},
{
Name: "compute",
URL: "https://my-custom-endpoint.example.com/compute/v2/",
},
},
},
valid: false,
},
{
name: "invalid gcp endpoint url blank",
platform: &gcp.Platform{
Region: "us-east1",
ServiceEndpoints: []gcp.ServiceEndpoint{
{
Name: "compute",
URL: "",
},
},
},
valid: false,
},
{
name: "invalid scheme gcp endpoint url",
platform: &gcp.Platform{
Region: "us-east1",
ServiceEndpoints: []gcp.ServiceEndpoint{
{
Name: "compute",
URL: "http://my-custom-endpoint.example.com/compute/v1/",
},
},
},
valid: false,
},
{
name: "valid gcp endpoint",
platform: &gcp.Platform{
Region: "us-east1",
ServiceEndpoints: []gcp.ServiceEndpoint{
{
Name: "compute",
URL: "https://my-custom-endpoint.example.com/compute/v1/",
},
},
},
valid: true,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit f42c756

Please sign in to comment.