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"
2021-11-26 02:34:48 +00:00
"os"
2022-09-05 04:01:14 +00:00
"strings"
"time"
2019-04-06 13:44:04 +00:00
2022-09-05 04:01:14 +00:00
"github.com/rs/zerolog/log"
2021-11-26 02:34:48 +00:00
"github.com/woodpecker-ci/woodpecker/pipeline/backend/types"
2022-12-31 00:37:09 +00:00
"gopkg.in/yaml.v3"
2022-09-05 04:01:14 +00:00
"github.com/urfave/cli/v2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
// To authenticate to GCP K8s clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
)
2023-03-19 19:24:43 +00:00
var noContext = context . Background ( )
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
ctx context . Context
client kubernetes . Interface
config * Config
2019-04-06 13:44:04 +00:00
}
2022-09-05 04:01:14 +00:00
type Config struct {
2022-12-31 00:37:09 +00:00
Namespace string
StorageClass string
VolumeSize string
2023-01-04 17:51:21 +00:00
StorageRwx bool
2022-12-31 00:37:09 +00:00
PodLabels map [ string ] string
PodAnnotations map [ string ] string
2022-09-05 04:01:14 +00:00
}
func configFromCliContext ( ctx context . Context ) ( * Config , error ) {
if ctx != nil {
if c , ok := ctx . Value ( types . CliContext ) . ( * cli . Context ) ; ok {
2022-12-31 00:37:09 +00:00
config := Config {
2023-01-04 17:51:21 +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
PodAnnotations : make ( map [ string ] string ) , // just init empty map to prevent nil panic
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 {
log . Error ( ) . Msgf ( "could not unmarshal pod labels '%s': %s" , c . String ( "backend-k8s-pod-labels" ) , err )
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 {
log . Error ( ) . Msgf ( "could not unmarshal pod annotations '%s': %s" , c . String ( "backend-k8s-pod-annotations" ) , err )
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
2019-04-06 13:44:04 +00:00
// New returns a new Kubernetes Engine.
2022-09-05 04:01:14 +00:00
func New ( ctx context . Context ) types . Engine {
2021-11-27 01:29:14 +00:00
return & kube {
2022-09-05 04:01:14 +00:00
ctx : ctx ,
2019-04-06 13:44:04 +00:00
}
}
2021-11-27 01:29:14 +00:00
func ( e * kube ) Name ( ) string {
2021-11-26 02:34:48 +00:00
return "kubernetes"
}
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
}
2023-03-19 19:24:43 +00:00
func ( e * kube ) Load ( context . Context ) error {
2022-09-05 04:01:14 +00:00
config , err := configFromCliContext ( e . ctx )
if err != nil {
return err
}
e . config = config
var kubeClient kubernetes . Interface
_ , err = rest . InClusterConfig ( )
if err != nil {
kubeClient , err = getClientOutOfCluster ( )
} else {
kubeClient , err = getClientInsideOfCluster ( )
}
if err != nil {
return err
}
e . client = kubeClient
2021-11-26 02:34:48 +00:00
return nil
}
2019-04-06 13:44:04 +00:00
// Setup 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-03-21 19:00:45 +00:00
pvc , err := PersistentVolumeClaim ( e . config . Namespace , vol . Name , e . config . StorageClass , e . config . VolumeSize , e . config . StorageRwx )
if err != nil {
return err
}
_ , err = e . client . CoreV1 ( ) . PersistentVolumeClaims ( e . config . Namespace ) . Create ( ctx , pvc , metav1 . CreateOptions { } )
2022-09-05 04:01:14 +00:00
if err != nil {
return err
}
}
extraHosts := [ ] string { }
for _ , stage := range conf . Stages {
if stage . Alias == "services" {
for _ , step := range stage . Steps {
2023-03-21 19:00:45 +00:00
stepName , err := dnsName ( step . Name )
if err != nil {
return err
}
log . Trace ( ) . Str ( "pod-name" , stepName ) . Msgf ( "Creating service: %s" , step . Name )
2022-09-05 04:01:14 +00:00
// TODO: support ports setting
2023-03-21 19:00:45 +00:00
// svc, err := Service(e.config.Namespace, step.Name, stepName, step.Ports)
svc , err := Service ( e . config . Namespace , step . Name , stepName , [ ] string { } )
2022-09-05 04:01:14 +00:00
if err != nil {
return err
}
svc , err = e . client . CoreV1 ( ) . Services ( e . config . Namespace ) . Create ( ctx , svc , metav1 . CreateOptions { } )
if err != nil {
return err
}
extraHosts = append ( extraHosts , step . Networks [ 0 ] . Aliases [ 0 ] + ":" + svc . Spec . ClusterIP )
}
}
}
log . Trace ( ) . Msgf ( "Adding extra hosts: %s" , strings . Join ( extraHosts , ", " ) )
for _ , stage := range conf . Stages {
for _ , step := range stage . Steps {
step . ExtraHosts = extraHosts
}
}
2019-04-06 13:44:04 +00:00
return nil
}
2022-09-05 04:01:14 +00:00
// Start the pipeline step.
2023-07-20 18:39:20 +00:00
func ( e * kube ) StartStep ( ctx context . Context , step * types . Step , taskUUID string ) error {
2023-03-21 19:00:45 +00:00
pod , err := Pod ( e . config . Namespace , step , e . config . PodLabels , e . config . PodAnnotations )
if err != nil {
return err
}
2023-07-20 18:39:20 +00:00
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "Creating pod: %s" , pod . Name )
2023-03-21 19:00:45 +00:00
_ , err = e . client . CoreV1 ( ) . Pods ( e . config . Namespace ) . Create ( ctx , pod , metav1 . CreateOptions { } )
2022-09-05 04:01:14 +00:00
return err
2019-04-06 13:44:04 +00:00
}
// Wait for the pipeline step to complete and returns
// 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 ) {
2023-03-21 19:00:45 +00:00
podName , err := dnsName ( step . Name )
if err != nil {
return nil , err
}
2022-09-05 04:01:14 +00:00
2023-07-20 18:39:20 +00:00
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "Waiting for pod: %s" , podName )
2022-09-05 04:01:14 +00:00
finished := make ( chan bool )
podUpdated := func ( old , new interface { } ) {
pod := new . ( * v1 . Pod )
if pod . Name == podName {
if isImagePullBackOffState ( pod ) {
finished <- true
}
switch pod . Status . Phase {
case v1 . PodSucceeded , v1 . PodFailed , v1 . PodUnknown :
finished <- true
}
}
}
// TODO 5 seconds is against best practice, k3s didn't work otherwise
si := informers . NewSharedInformerFactoryWithOptions ( e . client , 5 * time . Second , 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
// TODO Cancel on ctx.Done
<- finished
pod , err := e . client . CoreV1 ( ) . Pods ( e . config . Namespace ) . Get ( ctx , podName , metav1 . GetOptions { } )
if err != nil {
return nil , err
}
if isImagePullBackOffState ( pod ) {
return nil , fmt . Errorf ( "Could not pull image for pod %s" , pod . Name )
}
bs := & types . State {
ExitCode : int ( pod . Status . ContainerStatuses [ 0 ] . State . Terminated . ExitCode ) ,
Exited : true ,
OOMKilled : false ,
}
return bs , nil
2019-04-06 13:44:04 +00:00
}
// Tail 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 ) {
2023-03-21 19:00:45 +00:00
podName , err := dnsName ( step . Name )
if err != nil {
return nil , err
}
2022-09-05 04:01:14 +00:00
2023-07-20 18:39:20 +00:00
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msgf ( "Tail logs of pod: %s" , podName )
2022-09-05 04:01:14 +00:00
up := make ( chan bool )
podUpdated := func ( old , new interface { } ) {
pod := new . ( * v1 . Pod )
if pod . Name == podName {
switch pod . Status . Phase {
case v1 . PodRunning , v1 . PodSucceeded , v1 . PodFailed :
up <- true
}
}
}
// TODO 5 seconds is against best practice, k3s didn't work otherwise
si := informers . NewSharedInformerFactoryWithOptions ( e . client , 5 * time . Second , 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
// rc := io.NopCloser(bytes.NewReader(e.logs.Bytes()))
// e.logs.Reset()
// return rc, nil
2019-04-06 13:44:04 +00:00
}
// Destroy the pipeline environment.
2023-07-20 18:39:20 +00:00
func ( e * kube ) DestroyWorkflow ( _ context . Context , conf * types . Config , taskUUID string ) error {
log . Trace ( ) . Str ( "taskUUID" , taskUUID ) . Msg ( "Deleting Kubernetes primitives" )
2022-09-05 04:01:14 +00:00
gracePeriodSeconds := int64 ( 0 ) // immediately
dpb := metav1 . DeletePropagationBackground
deleteOpts := metav1 . DeleteOptions {
GracePeriodSeconds : & gracePeriodSeconds ,
PropagationPolicy : & dpb ,
}
// Use noContext because the ctx sent to this function will be canceled/done in case of error or canceled by user.
// Don't abort on 404 errors from k8s, they most likely mean that the pod hasn't been created yet, usually because pipeline was canceled before running all steps.
// Trace log them in case the info could be useful when troubleshooting.
for _ , stage := range conf . Stages {
for _ , step := range stage . Steps {
2023-03-21 19:00:45 +00:00
stepName , err := dnsName ( step . Name )
if err != nil {
return err
}
log . Trace ( ) . Msgf ( "Deleting pod: %s" , stepName )
if err := e . client . CoreV1 ( ) . Pods ( e . config . Namespace ) . Delete ( noContext , stepName , deleteOpts ) ; err != nil {
2022-09-05 04:01:14 +00:00
if errors . IsNotFound ( err ) {
2023-03-21 19:00:45 +00:00
log . Trace ( ) . Err ( err ) . Msgf ( "Unable to delete pod %s" , stepName )
2022-09-05 04:01:14 +00:00
} else {
return err
}
}
}
}
for _ , stage := range conf . Stages {
if stage . Alias == "services" {
for _ , step := range stage . Steps {
log . Trace ( ) . Msgf ( "Deleting service: %s" , step . Name )
// TODO: support ports setting
// svc, err := Service(e.config.Namespace, step.Name, step.Alias, step.Ports)
svc , err := Service ( e . config . Namespace , step . Name , step . Alias , [ ] string { } )
if err != nil {
return err
}
if err := e . client . CoreV1 ( ) . Services ( e . config . Namespace ) . Delete ( noContext , svc . Name , deleteOpts ) ; err != nil {
if errors . IsNotFound ( err ) {
2023-02-15 23:54:33 +00:00
log . Trace ( ) . Err ( err ) . Msgf ( "Unable to delete service %s" , svc . Name )
2022-09-05 04:01:14 +00:00
} else {
return err
}
}
}
}
}
for _ , vol := range conf . Volumes {
2023-03-21 19:00:45 +00:00
pvc , err := PersistentVolumeClaim ( e . config . Namespace , vol . Name , e . config . StorageClass , e . config . VolumeSize , e . config . StorageRwx )
if err != nil {
return err
}
err = e . client . CoreV1 ( ) . PersistentVolumeClaims ( e . config . Namespace ) . Delete ( noContext , pvc . Name , deleteOpts )
2022-09-05 04:01:14 +00:00
if err != nil {
if errors . IsNotFound ( err ) {
log . Trace ( ) . Err ( err ) . Msgf ( "Unable to delete pvc %s" , pvc . Name )
} else {
return err
}
}
}
2019-04-06 13:44:04 +00:00
return nil
}