mirror of https://github.com/kubernetes/kops.git
67 lines
1.8 KiB
Go
67 lines
1.8 KiB
Go
/*
|
|
Copyright 2022 The Kubernetes Authors.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
you may not use this file except in compliance with the License.
|
|
You may obtain a copy of the License at
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
See the License for the specific language governing permissions and
|
|
limitations under the License.
|
|
*/
|
|
|
|
package scaleway
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/scaleway/scaleway-sdk-go/scw"
|
|
"k8s.io/kops/pkg/apis/kops"
|
|
)
|
|
|
|
// isHTTPCodeError returns true if err is an http error with code statusCode
|
|
func isHTTPCodeError(err error, statusCode int) bool {
|
|
if err == nil {
|
|
return false
|
|
}
|
|
|
|
responseError := &scw.ResponseError{}
|
|
if errors.As(err, &responseError) && responseError.StatusCode == statusCode {
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
// is404Error returns true if err is an HTTP 404 error
|
|
func is404Error(err error) bool {
|
|
notFoundError := &scw.ResourceNotFoundError{}
|
|
return isHTTPCodeError(err, http.StatusNotFound) || errors.As(err, ¬FoundError)
|
|
}
|
|
|
|
func ParseZoneFromClusterSpec(clusterSpec kops.ClusterSpec) (scw.Zone, error) {
|
|
zone := ""
|
|
for _, subnet := range clusterSpec.Networking.Subnets {
|
|
if zone == "" {
|
|
zone = subnet.Zone
|
|
} else if zone != subnet.Zone {
|
|
return "", fmt.Errorf("scaleway currently only supports clusters in the same zone")
|
|
}
|
|
}
|
|
return scw.Zone(zone), nil
|
|
}
|
|
|
|
func ParseRegionFromZone(zone scw.Zone) (region scw.Region, err error) {
|
|
region, err = scw.ParseRegion(strings.TrimRight(string(zone), "-123"))
|
|
if err != nil {
|
|
return "", fmt.Errorf("could not determine region from zone %s: %w", zone, err)
|
|
}
|
|
return region, nil
|
|
}
|