2023-08-07 19:13:26 +00:00
|
|
|
// Copyright 2022 Woodpecker 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.
|
|
|
|
|
2022-09-05 04:01:14 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2023-03-21 19:00:45 +00:00
|
|
|
"errors"
|
2022-09-05 04:01:14 +00:00
|
|
|
"os"
|
2023-03-21 19:00:45 +00:00
|
|
|
"regexp"
|
2022-09-05 04:01:14 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/client-go/kubernetes"
|
|
|
|
"k8s.io/client-go/rest"
|
|
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
|
|
)
|
|
|
|
|
2023-03-21 19:00:45 +00:00
|
|
|
var (
|
2024-01-11 15:32:37 +00:00
|
|
|
dnsPattern = regexp.MustCompile(`^[a-z0-9]` + // must start with
|
|
|
|
`([-a-z0-9]*[a-z0-9])?` + // inside can als contain -
|
|
|
|
`(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$`, // allow the same pattern as before with dots in between but only one dot
|
|
|
|
)
|
2024-01-26 12:42:21 +00:00
|
|
|
dnsDisallowedCharacters = regexp.MustCompile(`[^-^.a-z0-9]+`)
|
|
|
|
ErrDNSPatternInvalid = errors.New("name is not a valid kubernetes DNS name")
|
2023-03-21 19:00:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func dnsName(i string) (string, error) {
|
2024-01-11 15:32:37 +00:00
|
|
|
res := strings.ToLower(strings.ReplaceAll(i, "_", "-"))
|
2023-03-21 19:00:45 +00:00
|
|
|
|
|
|
|
if found := dnsPattern.FindStringIndex(res); found == nil {
|
|
|
|
return "", ErrDNSPatternInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2022-09-05 04:01:14 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 12:42:21 +00:00
|
|
|
func toDNSName(in string) (string, error) {
|
|
|
|
lower := strings.ToLower(in)
|
|
|
|
withoutUnderscores := strings.ReplaceAll(lower, "_", "-")
|
|
|
|
withoutSpaces := strings.ReplaceAll(withoutUnderscores, " ", "-")
|
|
|
|
almostDNS := dnsDisallowedCharacters.ReplaceAllString(withoutSpaces, "")
|
|
|
|
return dnsName(almostDNS)
|
|
|
|
}
|
|
|
|
|
2022-09-05 04:01:14 +00:00
|
|
|
func isImagePullBackOffState(pod *v1.Pod) bool {
|
|
|
|
for _, containerState := range pod.Status.ContainerStatuses {
|
|
|
|
if containerState.State.Waiting != nil {
|
|
|
|
if containerState.State.Waiting.Reason == "ImagePullBackOff" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// getClientOutOfCluster returns a k8s clientset to the request from outside of cluster
|
|
|
|
func getClientOutOfCluster() (kubernetes.Interface, error) {
|
2023-08-07 19:13:26 +00:00
|
|
|
kubeConfigPath := os.Getenv("KUBECONFIG")
|
|
|
|
if kubeConfigPath == "" {
|
|
|
|
kubeConfigPath = os.Getenv("HOME") + "/.kube/config"
|
2022-09-05 04:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// use the current context in kubeconfig
|
2023-08-07 19:13:26 +00:00
|
|
|
config, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
|
2022-09-05 04:01:14 +00:00
|
|
|
if err != nil {
|
2023-03-20 23:48:15 +00:00
|
|
|
return nil, err
|
2022-09-05 04:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return kubernetes.NewForConfig(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getClient returns a k8s clientset to the request from inside of cluster
|
|
|
|
func getClientInsideOfCluster() (kubernetes.Interface, error) {
|
|
|
|
config, err := rest.InClusterConfig()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return kubernetes.NewForConfig(config)
|
|
|
|
}
|
2023-12-19 03:53:52 +00:00
|
|
|
|
|
|
|
func newBool(val bool) *bool {
|
|
|
|
ptr := new(bool)
|
|
|
|
*ptr = val
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
func newInt64(val int64) *int64 {
|
|
|
|
ptr := new(int64)
|
|
|
|
*ptr = val
|
|
|
|
return ptr
|
|
|
|
}
|