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
//
2023-08-10 09:06:00 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2023-08-07 19:13:26 +00:00
//
// 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.
2019-04-06 13:44:04 +00:00
package kubernetes
import (
"context"
2022-09-05 04:01:14 +00:00
"fmt"
2019-04-06 13:44:04 +00:00
"io"
2024-01-15 02:59:08 +00:00
"maps"
2021-11-26 02:34:48 +00:00
"os"
2023-11-01 14:38:37 +00:00
"runtime"
2024-01-15 02:59:08 +00:00
"slices"
2022-09-05 04:01:14 +00:00
"time"
2019-04-06 13:44:04 +00:00
2022-09-05 04:01:14 +00:00
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
2024-01-14 17:22:06 +00:00
"gopkg.in/yaml.v3"
2022-09-05 04:01:14 +00:00
v1 "k8s.io/api/core/v1"
2024-05-24 20:35:04 +00:00
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2022-09-05 04:01:14 +00:00
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
2024-05-13 20:58:21 +00:00
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // To authenticate to GCP K8s clusters
2022-09-05 04:01:14 +00:00
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
2024-01-14 17:22:06 +00:00
"go.woodpecker-ci.org/woodpecker/v2/pipeline/backend/types"
2022-09-05 04:01:14 +00:00
)
2023-11-01 23:53:47 +00:00
const (
EngineName = "kubernetes"
2024-05-13 20:58:21 +00:00
// TODO: 5 seconds is against best practice, k3s didn't work otherwise
2024-03-15 17:00:25 +00:00
defaultResyncDuration = 5 * time . Second
2023-11-01 23:53:47 +00:00
)
2023-12-19 03:53:52 +00:00
var defaultDeleteOptions = newDefaultDeleteOptions ( )
2019-04-06 13:44:04 +00:00
2021-11-27 01:29:14 +00:00
type kube struct {
2022-09-05 04:01:14 +00:00
client kubernetes . Interface
2023-12-19 03:53:52 +00:00
config * config
2023-11-01 14:38:37 +00:00
goos string
2019-04-06 13:44:04 +00:00
}
2023-12-19 03:53:52 +00:00
type config struct {
2024-05-11 09:45:29 +00:00
Namespace string
StorageClass string
VolumeSize string
StorageRwx bool
PodLabels map [ string ] string
PodLabelsAllowFromStep bool
PodAnnotations map [ string ] string
PodAnnotationsAllowFromStep bool
2024-06-03 15:25:28 +00:00
PodNodeSelector map [ string ] string
2024-05-11 09:45:29 +00:00
ImagePullSecretNames [ ] string
SecurityContext SecurityContextConfig
2024-06-23 16:20:21 +00:00
NativeSecretsAllowFromStep bool
2023-11-26 07:46:06 +00:00
}
type SecurityContextConfig struct {
RunAsNonRoot bool
2022-09-05 04:01:14 +00:00
}
2024-05-24 20:35:04 +00:00
func newDefaultDeleteOptions ( ) meta_v1 . DeleteOptions {
2023-12-19 03:53:52 +00:00
gracePeriodSeconds := int64 ( 0 ) // immediately
2024-05-24 20:35:04 +00:00
propagationPolicy := meta_v1 . DeletePropagationBackground
2023-12-19 03:53:52 +00:00
2024-05-24 20:35:04 +00:00
return meta_v1 . DeleteOptions {
2023-12-19 03:53:52 +00:00
GracePeriodSeconds : & gracePeriodSeconds ,
PropagationPolicy : & propagationPolicy ,
}
}
func configFromCliContext ( ctx context . Context ) ( * config , error ) {
2022-09-05 04:01:14 +00:00
if ctx != nil {
if c , ok := ctx . Value ( types . CliContext ) . ( * cli . Context ) ; ok {
2023-12-19 03:53:52 +00:00
config := config {
2024-05-11 09:45:29 +00:00
Namespace : c . String ( "backend-k8s-namespace" ) ,
StorageClass : c . String ( "backend-k8s-storage-class" ) ,
VolumeSize : c . String ( "backend-k8s-volume-size" ) ,
StorageRwx : c . Bool ( "backend-k8s-storage-rwx" ) ,
PodLabels : make ( map [ string ] string ) , // just init empty map to prevent nil panic
PodLabelsAllowFromStep : c . Bool ( "backend-k8s-pod-labels-allow-from-step" ) ,
PodAnnotations : make ( map [ string ] string ) , // just init empty map to prevent nil panic
PodAnnotationsAllowFromStep : c . Bool ( "backend-k8s-pod-annotations-allow-from-step" ) ,
2024-06-03 15:25:28 +00:00
PodNodeSelector : make ( map [ string ] string ) , // just init empty map to prevent nil panic
2024-05-11 09:45:29 +00:00
ImagePullSecretNames : c . StringSlice ( "backend-k8s-pod-image-pull-secret-names" ) ,
2023-11-26 07:46:06 +00:00
SecurityContext : SecurityContextConfig {
2024-05-24 20:35:04 +00:00
RunAsNonRoot : c . Bool ( "backend-k8s-secctx-nonroot" ) , // cspell:words secctx nonroot
2023-11-26 07:46:06 +00:00
} ,
2024-06-23 16:20:21 +00:00
NativeSecretsAllowFromStep : c . Bool ( "backend-k8s-allow-native-secrets" ) ,
2022-12-31 00:37:09 +00:00
}
2024-01-05 07:33:56 +00:00
// TODO: remove in next major
if len ( config . ImagePullSecretNames ) == 1 && config . ImagePullSecretNames [ 0 ] == "regcred" {
log . Warn ( ) . Msg ( "WOODPECKER_BACKEND_K8S_PULL_SECRET_NAMES is set to the default ('regcred'). It will default to empty in Woodpecker 3.0. Set it explicitly before then." )
}
2022-12-31 00:37:09 +00:00
// Unmarshal label and annotation settings here to ensure they're valid on startup
2023-01-04 17:51:21 +00:00
if labels := c . String ( "backend-k8s-pod-labels" ) ; labels != "" {
if err := yaml . Unmarshal ( [ ] byte ( labels ) , & config . PodLabels ) ; err != nil {
2024-01-10 19:57:12 +00:00
log . Error ( ) . Err ( err ) . Msgf ( "could not unmarshal pod labels '%s'" , c . String ( "backend-k8s-pod-labels" ) )
2023-01-04 17:51:21 +00:00
return nil , err
}
2022-12-31 00:37:09 +00:00
}
2023-01-04 17:51:21 +00:00
if annotations := c . String ( "backend-k8s-pod-annotations" ) ; annotations != "" {
if err := yaml . Unmarshal ( [ ] byte ( c . String ( "backend-k8s-pod-annotations" ) ) , & config . PodAnnotations ) ; err != nil {
2024-01-10 19:57:12 +00:00
log . Error ( ) . Err ( err ) . Msgf ( "could not unmarshal pod annotations '%s'" , c . String ( "backend-k8s-pod-annotations" ) )
2023-01-04 17:51:21 +00:00
return nil , err
}
2022-12-31 00:37:09 +00:00
}
2024-06-03 15:25:28 +00:00
if nodeSelector := c . String ( "backend-k8s-pod-node-selector" ) ; nodeSelector != "" {
if err := yaml . Unmarshal ( [ ] byte ( nodeSelector ) , & config . PodNodeSelector ) ; err != nil {
log . Error ( ) . Err ( err ) . Msgf ( "could not unmarshal pod node selector '%s'" , nodeSelector )
return nil , err
}
}
2022-12-31 00:37:09 +00:00
return & config , nil
2022-09-05 04:01:14 +00:00
}
}
2023-03-19 19:24:43 +00:00
return nil , types . ErrNoCliContextFound
2022-09-05 04:01:14 +00:00
}
2021-11-27 01:29:14 +00:00
2023-12-14 18:20:47 +00:00
// New returns a new Kubernetes Backend.
2024-01-11 21:15:15 +00:00
func New ( ) types . Backend {
return & kube { }
2019-04-06 13:44:04 +00:00
}
2021-11-27 01:29:14 +00:00
func ( e * kube ) Name ( ) string {
2023-11-01 23:53:47 +00:00
return EngineName
2021-11-26 02:34:48 +00:00
}
2023-03-19 19:24:43 +00:00
func ( e * kube ) IsAvailable ( context . Context ) bool {
2021-11-26 02:34:48 +00:00
host := os . Getenv ( "KUBERNETES_SERVICE_HOST" )
return len ( host ) > 0
}
2024-02-08 15:33:22 +00:00
func ( e * kube ) Flags ( ) [ ] cli . Flag {
return Flags
}
2024-01-11 21:15:15 +00:00
func ( e * kube ) Load ( ctx context . Context ) ( * types . BackendInfo , error ) {
config , err := configFromCliContext ( ctx )
2022-09-05 04:01:14 +00:00
if err != nil {
2023-11-01 14:38:37 +00:00
return nil , err
2022-09-05 04:01:14 +00:00
}
e . config = config
var kubeClient kubernetes . Interface
_ , err = rest . InClusterConfig ( )
if err != nil {
kubeClient , err = getClientOutOfCluster ( )
} else {
kubeClient , err = getClientInsideOfCluster ( )
}
if err != nil {
2023-11-01 14:38:37 +00:00
return nil , err
2022-09-05 04:01:14 +00:00
}
e . client = kubeClient
2023-11-01 14:38:37 +00:00
// TODO(2693): use info resp of kubeClient to define platform var
e . goos = runtime . GOOS
2023-12-14 18:20:47 +00:00
return & types . BackendInfo {
2023-11-01 14:38:37 +00:00
Platform : runtime . GOOS + "/" + runtime . GOARCH ,
} , nil
2021-11-26 02:34:48 +00:00
}
2024-01-15 02:59:08 +00:00
func ( e * kube ) getConfig ( ) * config {
if e . config == nil {
return nil
}
c := * e . config
c . PodLabels = maps . Clone ( e . config . PodLabels )
2024-01-22 12:39:49 +00:00
c . PodAnnotations = maps . Clone ( e . config . PodAnnotations )
2024-06-03 15:25:28 +00:00
c . PodNodeSelector = maps . Clone ( e . config . PodNodeSelector )
2024-01-15 02:59:08 +00:00
c . ImagePullSecretNames = slices . Clone ( e . config . ImagePullSecretNames )
return & c
}
2024-02-08 17:39:32 +00:00
// SetupWorkflow sets up the pipeline environment.
2023-07-20 18:39:20 +00:00
func ( e * kube ) SetupWorkflow ( ctx context . Context , conf * types . Config , taskUUID string ) error {
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "Setting up Kubernetes primitives" )
2022-09-05 04:01:14 +00:00
for _ , vol := range conf . Volumes {
2023-12-19 03:53:52 +00:00
_ , err := startVolume ( ctx , e , vol . Name )
2022-09-05 04:01:14 +00:00
if err != nil {
return err
}
}
2024-02-08 17:39:32 +00:00
var extraHosts [ ] types . HostAlias
2022-09-05 04:01:14 +00:00
for _ , stage := range conf . Stages {
2024-01-09 04:42:36 +00:00
for _ , step := range stage . Steps {
if step . Type == types . StepTypeService {
2023-12-19 03:53:52 +00:00
svc , err := startService ( ctx , e , step )
2023-03-21 19:00:45 +00:00
if err != nil {
return err
}
2023-12-22 23:42:30 +00:00
hostAlias := types . HostAlias { Name : step . Networks [ 0 ] . Aliases [ 0 ] , IP : svc . Spec . ClusterIP }
extraHosts = append ( extraHosts , hostAlias )
2022-09-05 04:01:14 +00:00
}
}
}
2024-01-11 18:17:07 +00:00
log . Trace ( ) . Msgf ( "adding extra hosts: %v" , extraHosts )
2022-09-05 04:01:14 +00:00
for _ , stage := range conf . Stages {
for _ , step := range stage . Steps {
step . ExtraHosts = extraHosts
}
}
2019-04-06 13:44:04 +00:00
return nil
}
2024-02-08 17:39:32 +00:00
// StartStep starts the pipeline step.
2023-07-20 18:39:20 +00:00
func ( e * kube ) StartStep ( ctx context . Context , step * types . Step , taskUUID string ) error {
2024-02-08 17:39:32 +00:00
options , err := parseBackendOptions ( step )
if err != nil {
log . Error ( ) . Err ( err ) . Msg ( "could not parse backend options" )
}
2024-01-11 18:17:07 +00:00
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "starting step: %s" , step . Name )
2024-02-08 17:39:32 +00:00
_ , err = startPod ( ctx , e , step , options )
2022-09-05 04:01:14 +00:00
return err
2019-04-06 13:44:04 +00:00
}
2024-02-08 17:39:32 +00:00
// WaitStep waits for the pipeline step to complete and returns
2019-04-06 13:44:04 +00:00
// the completion results.
2023-07-20 18:39:20 +00:00
func ( e * kube ) WaitStep ( ctx context . Context , step * types . Step , taskUUID string ) ( * types . State , error ) {
2024-01-09 04:42:36 +00:00
podName , err := stepToPodName ( step )
2023-03-21 19:00:45 +00:00
if err != nil {
return nil , err
}
2022-09-05 04:01:14 +00:00
2024-01-11 18:17:07 +00:00
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "waiting for pod: %s" , podName )
2023-07-20 18:39:20 +00:00
2022-09-05 04:01:14 +00:00
finished := make ( chan bool )
2024-02-08 21:49:07 +00:00
podUpdated := func ( _ , new any ) {
2024-01-12 01:01:02 +00:00
pod , ok := new . ( * v1 . Pod )
if ! ok {
log . Error ( ) . Msgf ( "could not parse pod: %v" , new )
return
}
2022-09-05 04:01:14 +00:00
if pod . Name == podName {
if isImagePullBackOffState ( pod ) {
finished <- true
}
switch pod . Status . Phase {
case v1 . PodSucceeded , v1 . PodFailed , v1 . PodUnknown :
finished <- true
}
}
}
2024-03-15 17:00:25 +00:00
si := informers . NewSharedInformerFactoryWithOptions ( e . client , defaultResyncDuration , informers . WithNamespace ( e . config . Namespace ) )
2023-03-20 23:48:15 +00:00
if _ , err := si . Core ( ) . V1 ( ) . Pods ( ) . Informer ( ) . AddEventHandler (
2022-09-05 04:01:14 +00:00
cache . ResourceEventHandlerFuncs {
UpdateFunc : podUpdated ,
} ,
2023-03-20 23:48:15 +00:00
) ; err != nil {
return nil , err
}
2023-02-15 23:54:33 +00:00
stop := make ( chan struct { } )
si . Start ( stop )
defer close ( stop )
2022-09-05 04:01:14 +00:00
2024-05-13 20:58:21 +00:00
// TODO: Cancel on ctx.Done
2022-09-05 04:01:14 +00:00
<- finished
2024-05-24 20:35:04 +00:00
pod , err := e . client . CoreV1 ( ) . Pods ( e . config . Namespace ) . Get ( ctx , podName , meta_v1 . GetOptions { } )
2022-09-05 04:01:14 +00:00
if err != nil {
return nil , err
}
if isImagePullBackOffState ( pod ) {
2024-02-05 21:46:14 +00:00
return nil , fmt . Errorf ( "could not pull image for pod %s" , podName )
}
if len ( pod . Status . ContainerStatuses ) == 0 {
return nil , fmt . Errorf ( "no container statuses found for pod %s" , podName )
}
cs := pod . Status . ContainerStatuses [ 0 ]
if cs . State . Terminated == nil {
err := fmt . Errorf ( "no terminated state found for container %s/%s" , podName , cs . Name )
log . Error ( ) . Str ( "taskUUID" , taskUUID ) . Str ( "pod" , podName ) . Str ( "container" , cs . Name ) . Interface ( "state" , cs . State ) . Msg ( err . Error ( ) )
return nil , err
2022-09-05 04:01:14 +00:00
}
bs := & types . State {
2024-02-05 21:46:14 +00:00
ExitCode : int ( cs . State . Terminated . ExitCode ) ,
2022-09-05 04:01:14 +00:00
Exited : true ,
OOMKilled : false ,
}
return bs , nil
2019-04-06 13:44:04 +00:00
}
2024-02-08 17:39:32 +00:00
// TailStep tails the pipeline step logs.
2023-07-20 18:39:20 +00:00
func ( e * kube ) TailStep ( ctx context . Context , step * types . Step , taskUUID string ) ( io . ReadCloser , error ) {
2024-01-09 04:42:36 +00:00
podName , err := stepToPodName ( step )
2023-03-21 19:00:45 +00:00
if err != nil {
return nil , err
}
2022-09-05 04:01:14 +00:00
2024-01-11 18:17:07 +00:00
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "tail logs of pod: %s" , podName )
2023-07-20 18:39:20 +00:00
2022-09-05 04:01:14 +00:00
up := make ( chan bool )
2024-02-08 21:49:07 +00:00
podUpdated := func ( _ , new any ) {
2024-01-12 01:01:02 +00:00
pod , ok := new . ( * v1 . Pod )
if ! ok {
log . Error ( ) . Msgf ( "could not parse pod: %v" , new )
return
}
2022-09-05 04:01:14 +00:00
if pod . Name == podName {
2024-04-15 07:08:13 +00:00
if isImagePullBackOffState ( pod ) {
up <- true
}
2022-09-05 04:01:14 +00:00
switch pod . Status . Phase {
case v1 . PodRunning , v1 . PodSucceeded , v1 . PodFailed :
up <- true
}
}
}
2024-03-15 17:00:25 +00:00
si := informers . NewSharedInformerFactoryWithOptions ( e . client , defaultResyncDuration , informers . WithNamespace ( e . config . Namespace ) )
2023-03-20 23:48:15 +00:00
if _ , err := si . Core ( ) . V1 ( ) . Pods ( ) . Informer ( ) . AddEventHandler (
2022-09-05 04:01:14 +00:00
cache . ResourceEventHandlerFuncs {
UpdateFunc : podUpdated ,
} ,
2023-03-20 23:48:15 +00:00
) ; err != nil {
return nil , err
}
2023-02-15 23:54:33 +00:00
stop := make ( chan struct { } )
si . Start ( stop )
defer close ( stop )
2022-09-05 04:01:14 +00:00
<- up
opts := & v1 . PodLogOptions {
2022-12-31 00:37:09 +00:00
Follow : true ,
Container : podName ,
2022-09-05 04:01:14 +00:00
}
logs , err := e . client . CoreV1 ( ) . RESTClient ( ) . Get ( ) .
Namespace ( e . config . Namespace ) .
Name ( podName ) .
Resource ( "pods" ) .
SubResource ( "log" ) .
VersionedParams ( opts , scheme . ParameterCodec ) .
Stream ( ctx )
if err != nil {
return nil , err
}
rc , wc := io . Pipe ( )
go func ( ) {
defer logs . Close ( )
defer wc . Close ( )
defer rc . Close ( )
_ , err = io . Copy ( wc , logs )
if err != nil {
return
}
} ( )
return rc , nil
2019-04-06 13:44:04 +00:00
}
2024-01-11 21:15:15 +00:00
func ( e * kube ) DestroyStep ( ctx context . Context , step * types . Step , taskUUID string ) error {
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "Stopping step: %s" , step . Name )
err := stopPod ( ctx , e , step , defaultDeleteOptions )
2023-12-19 03:53:52 +00:00
return err
2023-11-01 08:35:11 +00:00
}
2024-02-08 17:39:32 +00:00
// DestroyWorkflow destroys the pipeline environment.
2024-01-11 21:15:15 +00:00
func ( e * kube ) DestroyWorkflow ( ctx context . Context , conf * types . Config , taskUUID string ) error {
2024-01-11 18:17:07 +00:00
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msg ( "deleting Kubernetes primitives" )
2023-07-20 18:39:20 +00:00
2022-09-05 04:01:14 +00:00
// Use noContext because the ctx sent to this function will be canceled/done in case of error or canceled by user.
for _ , stage := range conf . Stages {
for _ , step := range stage . Steps {
2024-01-11 21:15:15 +00:00
err := stopPod ( ctx , e , step , defaultDeleteOptions )
2023-03-21 19:00:45 +00:00
if err != nil {
return err
}
2022-09-05 04:01:14 +00:00
2023-12-19 03:53:52 +00:00
if step . Type == types . StepTypeService {
2024-01-11 21:15:15 +00:00
err := stopService ( ctx , e , step , defaultDeleteOptions )
2022-09-05 04:01:14 +00:00
if err != nil {
return err
}
}
}
}
for _ , vol := range conf . Volumes {
2024-01-11 21:15:15 +00:00
err := stopVolume ( ctx , e , vol . Name , defaultDeleteOptions )
2023-03-21 19:00:45 +00:00
if err != nil {
return err
}
2022-09-05 04:01:14 +00:00
}
2019-04-06 13:44:04 +00:00
return nil
}