2022-09-05 03:06:08 -04:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
2022-08-12 04:20:19 -04:00
package helm
import (
"bytes"
"embed"
2022-10-21 06:01:28 -04:00
"encoding/base64"
2022-08-12 04:20:19 -04:00
"encoding/json"
"fmt"
"io/fs"
2022-10-21 06:01:28 -04:00
"os"
2022-08-12 04:20:19 -04:00
"path/filepath"
"strings"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
2022-11-18 04:05:02 -05:00
"github.com/edgelesssys/constellation/v2/internal/config"
2022-10-18 07:15:54 -04:00
"github.com/edgelesssys/constellation/v2/internal/constants"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/deploy/helm"
2022-10-18 07:15:54 -04:00
"github.com/edgelesssys/constellation/v2/internal/versions"
2022-08-12 04:20:19 -04:00
"github.com/pkg/errors"
"helm.sh/helm/pkg/ignore"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
2022-10-21 06:01:28 -04:00
"helm.sh/helm/v3/pkg/chartutil"
2022-08-12 04:20:19 -04:00
)
2022-11-21 04:35:40 -05:00
// Run `go generate` to download (and patch) upstream helm charts.
2022-08-12 04:20:19 -04:00
//go:generate ./generateCilium.sh
2022-11-18 04:05:02 -05:00
//go:generate ./update-csi-charts.sh
2022-11-21 04:35:40 -05:00
//go:generate ./generateCertManager.sh
2022-08-12 04:20:19 -04:00
2022-10-18 07:15:54 -04:00
//go:embed all:charts/*
2022-11-09 09:57:54 -05:00
var helmFS embed . FS
2022-08-12 04:20:19 -04:00
2022-11-09 09:57:54 -05:00
// ChartLoader loads embedded helm charts.
2022-10-31 14:25:02 -04:00
type ChartLoader struct {
2022-11-21 11:06:41 -05:00
joinServiceImage string
kmsImage string
ccmImage string
cnmImage string
autoscalerImage string
verificationServiceImage string
}
type LoadConfig struct {
Csp cloudprovider . Provider
ConformanceMode bool
MasterSecret [ ] byte
Salt [ ] byte
2022-10-31 14:25:02 -04:00
}
2022-11-09 09:57:54 -05:00
// New creates a new ChartLoader.
2022-10-31 14:25:02 -04:00
func New ( csp cloudprovider . Provider , k8sVersion versions . ValidK8sVersion ) * ChartLoader {
2022-11-02 12:47:10 -04:00
var ccmImage , cnmImage string
2022-10-31 14:25:02 -04:00
switch csp {
case cloudprovider . AWS :
ccmImage = versions . VersionConfigs [ k8sVersion ] . CloudControllerManagerImageAWS
case cloudprovider . Azure :
ccmImage = versions . VersionConfigs [ k8sVersion ] . CloudControllerManagerImageAzure
2022-11-02 12:47:10 -04:00
cnmImage = versions . VersionConfigs [ k8sVersion ] . CloudNodeManagerImageAzure
2022-10-31 14:25:02 -04:00
case cloudprovider . GCP :
ccmImage = versions . VersionConfigs [ k8sVersion ] . CloudControllerManagerImageGCP
}
2022-08-12 04:20:19 -04:00
2022-10-31 14:25:02 -04:00
return & ChartLoader {
2022-11-21 11:06:41 -05:00
joinServiceImage : versions . JoinImage ,
kmsImage : versions . KmsImage ,
ccmImage : ccmImage ,
cnmImage : cnmImage ,
autoscalerImage : versions . VersionConfigs [ k8sVersion ] . ClusterAutoscalerImage ,
verificationServiceImage : versions . VerificationImage ,
2022-10-31 14:25:02 -04:00
}
}
2022-11-09 09:57:54 -05:00
// Load the embedded helm charts.
2022-11-18 04:05:02 -05:00
func ( i * ChartLoader ) Load ( config * config . Config , conformanceMode bool , masterSecret , salt [ ] byte ) ( [ ] byte , error ) {
csp := config . GetProvider ( )
2022-10-18 07:15:54 -04:00
ciliumRelease , err := i . loadCilium ( csp , conformanceMode )
2022-08-12 04:20:19 -04:00
if err != nil {
2022-10-21 11:09:43 -04:00
return nil , fmt . Errorf ( "loading cilium: %w" , err )
2022-08-12 04:20:19 -04:00
}
2022-10-18 07:15:54 -04:00
2022-11-21 04:35:40 -05:00
certManagerRelease , err := i . loadCertManager ( )
if err != nil {
return nil , fmt . Errorf ( "loading cilium: %w" , err )
}
operatorRelease , err := i . loadOperators ( csp )
if err != nil {
return nil , fmt . Errorf ( "loading operators: %w" , err )
}
2022-11-18 04:05:02 -05:00
conServicesRelease , err := i . loadConstellationServices ( config , masterSecret , salt )
2022-08-12 04:20:19 -04:00
if err != nil {
2022-10-21 11:09:43 -04:00
return nil , fmt . Errorf ( "loading constellation-services: %w" , err )
2022-08-12 04:20:19 -04:00
}
2022-11-21 04:35:40 -05:00
releases := helm . Releases { Cilium : ciliumRelease , CertManager : certManagerRelease , Operators : operatorRelease , ConstellationServices : conServicesRelease }
2022-10-21 06:01:28 -04:00
2022-10-18 07:15:54 -04:00
rel , err := json . Marshal ( releases )
if err != nil {
return nil , err
}
return rel , nil
2022-08-12 04:20:19 -04:00
}
2022-10-18 07:15:54 -04:00
func ( i * ChartLoader ) loadCilium ( csp cloudprovider . Provider , conformanceMode bool ) ( helm . Release , error ) {
2022-11-21 04:35:40 -05:00
chart , values , err := i . loadCiliumHelper ( csp , conformanceMode )
2022-08-12 04:20:19 -04:00
if err != nil {
2022-11-21 04:35:40 -05:00
return helm . Release { } , err
2022-10-21 06:01:28 -04:00
}
chartRaw , err := i . marshalChart ( chart )
if err != nil {
2022-11-21 04:35:40 -05:00
return helm . Release { } , fmt . Errorf ( "packaging cilium chart: %w" , err )
2022-08-12 04:20:19 -04:00
}
2022-10-21 06:01:28 -04:00
2022-11-21 04:35:40 -05:00
return helm . Release { Chart : chartRaw , Values : values , ReleaseName : "cilium" , Wait : false } , nil
}
// loadCiliumHelper is used to separate the marshalling step from the loading step.
// This reduces the time unit tests take to execute.
func ( i * ChartLoader ) loadCiliumHelper ( csp cloudprovider . Provider , conformanceMode bool ) ( * chart . Chart , map [ string ] any , error ) {
chart , err := loadChartsDir ( helmFS , "charts/cilium" )
if err != nil {
return nil , nil , fmt . Errorf ( "loading cilium chart: %w" , err )
}
var values map [ string ] any
2022-10-07 03:38:43 -04:00
switch csp {
2022-10-21 08:41:31 -04:00
case cloudprovider . AWS :
2022-11-21 04:35:40 -05:00
values = awsVals
2022-08-24 03:45:02 -04:00
case cloudprovider . Azure :
2022-11-21 04:35:40 -05:00
values = azureVals
2022-10-21 08:41:31 -04:00
case cloudprovider . GCP :
2022-11-21 04:35:40 -05:00
values = gcpVals
2022-08-24 03:45:02 -04:00
case cloudprovider . QEMU :
2022-11-21 04:35:40 -05:00
values = qemuVals
2022-08-12 04:20:19 -04:00
default :
2022-11-21 04:35:40 -05:00
return nil , nil , fmt . Errorf ( "unknown csp: %s" , csp )
2022-08-12 04:20:19 -04:00
}
2022-09-20 04:07:55 -04:00
if conformanceMode {
2022-11-21 04:35:40 -05:00
values [ "kubeProxyReplacementHealthzBindAddr" ] = ""
values [ "kubeProxyReplacement" ] = "partial"
values [ "sessionAffinity" ] = true
values [ "cni" ] = map [ string ] any {
2022-09-20 04:07:55 -04:00
"chainingMode" : "portmap" ,
}
}
2022-11-21 04:35:40 -05:00
return chart , values , nil
}
func ( i * ChartLoader ) loadCertManager ( ) ( helm . Release , error ) {
chart , values , err := i . loadCertManagerHelper ( )
if err != nil {
return helm . Release { } , err
}
chartRaw , err := i . marshalChart ( chart )
if err != nil {
return helm . Release { } , fmt . Errorf ( "packaging cert-manager chart: %w" , err )
}
return helm . Release { Chart : chartRaw , Values : values , ReleaseName : "cert-manager" , Wait : false } , nil
}
// loadCertManagerHelper is used to separate the marshalling step from the loading step.
// This reduces the time unit tests take to execute.
func ( i * ChartLoader ) loadCertManagerHelper ( ) ( * chart . Chart , map [ string ] any , error ) {
chart , err := loadChartsDir ( helmFS , "charts/cert-manager" )
if err != nil {
return nil , nil , fmt . Errorf ( "loading cert-manager chart: %w" , err )
}
values := map [ string ] any {
"installCRDs" : true ,
"prometheus" : map [ string ] any {
"enabled" : false ,
} ,
"tolerations" : [ ] map [ string ] any {
{
"key" : "node-role.kubernetes.io/control-plane" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
{
"key" : "node-role.kubernetes.io/master" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
} ,
"webhook" : map [ string ] any {
"tolerations" : [ ] map [ string ] any {
{
"key" : "node-role.kubernetes.io/control-plane" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
{
"key" : "node-role.kubernetes.io/master" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
} ,
} ,
"cainjector" : map [ string ] any {
"tolerations" : [ ] map [ string ] any {
{
"key" : "node-role.kubernetes.io/control-plane" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
{
"key" : "node-role.kubernetes.io/master" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
} ,
} ,
"startupapicheck" : map [ string ] any {
"timeout" : "5m" ,
"tolerations" : [ ] map [ string ] any {
{
"key" : "node-role.kubernetes.io/control-plane" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
{
"key" : "node-role.kubernetes.io/master" ,
"effect" : "NoSchedule" ,
"operator" : "Exists" ,
} ,
} ,
} ,
}
return chart , values , nil
}
func ( i * ChartLoader ) loadOperators ( csp cloudprovider . Provider ) ( helm . Release , error ) {
chart , values , err := i . loadOperatorsHelper ( csp )
if err != nil {
return helm . Release { } , err
}
chartRaw , err := i . marshalChart ( chart )
if err != nil {
return helm . Release { } , fmt . Errorf ( "packaging operators chart: %w" , err )
}
return helm . Release { Chart : chartRaw , Values : values , ReleaseName : "con-operators" , Wait : false } , nil
}
// loadOperatorsHelper is used to separate the marshalling step from the loading step.
// This reduces the time unit tests take to execute.
func ( i * ChartLoader ) loadOperatorsHelper ( csp cloudprovider . Provider ) ( * chart . Chart , map [ string ] any , error ) {
chart , err := loadChartsDir ( helmFS , "charts/edgeless/operators" )
if err != nil {
return nil , nil , fmt . Errorf ( "loading operators chart: %w" , err )
}
values := map [ string ] any {
"constellation-operator" : map [ string ] any {
"controllerManager" : map [ string ] any {
"manager" : map [ string ] any {
"image" : versions . ConstellationOperatorImage ,
} ,
} ,
} ,
"node-maintenance-operator" : map [ string ] any {
"controllerManager" : map [ string ] any {
"manager" : map [ string ] any {
"image" : versions . NodeMaintenanceOperatorImage ,
} ,
} ,
} ,
}
switch csp {
case cloudprovider . Azure :
conOpVals , ok := values [ "constellation-operator" ] . ( map [ string ] any )
if ! ok {
return nil , nil , errors . New ( "invalid constellation-operator values" )
}
conOpVals [ "csp" ] = "Azure"
values [ "tags" ] = map [ string ] any {
"Azure" : true ,
}
case cloudprovider . GCP :
conOpVals , ok := values [ "constellation-operator" ] . ( map [ string ] any )
if ! ok {
return nil , nil , errors . New ( "invalid constellation-operator values" )
}
conOpVals [ "csp" ] = "GCP"
values [ "tags" ] = map [ string ] any {
"GCP" : true ,
}
case cloudprovider . QEMU :
conOpVals , ok := values [ "constellation-operator" ] . ( map [ string ] any )
if ! ok {
return nil , nil , errors . New ( "invalid constellation-operator values" )
}
conOpVals [ "csp" ] = "QEMU"
values [ "tags" ] = map [ string ] any {
"QEMU" : true ,
}
case cloudprovider . AWS :
conOpVals , ok := values [ "constellation-operator" ] . ( map [ string ] any )
if ! ok {
return nil , nil , errors . New ( "invalid constellation-operator values" )
}
conOpVals [ "csp" ] = "AWS"
values [ "tags" ] = map [ string ] any {
"AWS" : true ,
}
}
2022-09-20 04:07:55 -04:00
2022-11-21 04:35:40 -05:00
return chart , values , nil
2022-10-18 07:15:54 -04:00
}
2022-11-18 04:05:02 -05:00
// loadConstellationServices loads the constellation-services chart from the embed.FS,
// marshals it into a helm-package .tgz and sets the values that can be set in the CLI.
func ( i * ChartLoader ) loadConstellationServices ( config * config . Config , masterSecret , salt [ ] byte ) ( helm . Release , error ) {
2022-11-21 04:35:40 -05:00
chart , values , err := i . loadConstellationServicesHelper ( config , masterSecret , salt )
2022-10-18 07:15:54 -04:00
if err != nil {
2022-11-21 04:35:40 -05:00
return helm . Release { } , err
2022-10-18 07:15:54 -04:00
}
2022-10-21 06:01:28 -04:00
chartRaw , err := i . marshalChart ( chart )
if err != nil {
2022-11-21 04:35:40 -05:00
return helm . Release { } , fmt . Errorf ( "packaging constellation-services chart: %w" , err )
}
return helm . Release { Chart : chartRaw , Values : values , ReleaseName : "constellation-services" , Wait : false } , nil
}
// loadConstellationServicesHelper is used to separate the marshalling step from the loading step.
// This reduces the time unit tests take to execute.
func ( i * ChartLoader ) loadConstellationServicesHelper ( config * config . Config , masterSecret , salt [ ] byte ) ( * chart . Chart , map [ string ] any , error ) {
chart , err := loadChartsDir ( helmFS , "charts/edgeless/constellation-services" )
if err != nil {
return nil , nil , fmt . Errorf ( "loading constellation-services chart: %w" , err )
2022-10-18 07:15:54 -04:00
}
2022-11-18 04:05:02 -05:00
enforcedPCRsJSON , err := json . Marshal ( config . GetEnforcedPCRs ( ) )
2022-10-24 06:23:18 -04:00
if err != nil {
2022-11-21 04:35:40 -05:00
return nil , nil , fmt . Errorf ( "marshaling enforcedPCRs: %w" , err )
2022-10-24 06:23:18 -04:00
}
2022-11-18 04:05:02 -05:00
csp := config . GetProvider ( )
2022-11-21 04:35:40 -05:00
values := map [ string ] any {
2022-10-25 09:51:23 -04:00
"global" : map [ string ] any {
2022-10-24 06:23:18 -04:00
"kmsPort" : constants . KMSPort ,
"serviceBasePath" : constants . ServiceBasePath ,
"joinConfigCMName" : constants . JoinConfigMap ,
"k8sVersionCMName" : constants . K8sVersion ,
"internalCMName" : constants . InternalConfigMap ,
} ,
2022-10-25 09:51:23 -04:00
"kms" : map [ string ] any {
2022-10-31 14:25:02 -04:00
"image" : i . kmsImage ,
2022-10-21 06:01:28 -04:00
"masterSecret" : base64 . StdEncoding . EncodeToString ( masterSecret ) ,
"salt" : base64 . StdEncoding . EncodeToString ( salt ) ,
2022-10-24 06:23:18 -04:00
"saltKeyName" : constants . ConstellationSaltKey ,
"masterSecretKeyName" : constants . ConstellationMasterSecretKey ,
"masterSecretName" : constants . ConstellationMasterSecretStoreName ,
"measurementsFilename" : constants . MeasurementsFilename ,
} ,
2022-10-25 09:51:23 -04:00
"join-service" : map [ string ] any {
2022-11-21 04:35:40 -05:00
"csp" : csp . String ( ) ,
2022-10-24 06:23:18 -04:00
"enforcedPCRs" : string ( enforcedPCRsJSON ) ,
2022-10-31 14:25:02 -04:00
"image" : i . joinServiceImage ,
2022-10-21 06:01:28 -04:00
} ,
2022-11-02 12:47:10 -04:00
"ccm" : map [ string ] any {
2022-11-21 04:35:40 -05:00
"csp" : csp . String ( ) ,
2022-10-26 04:37:10 -04:00
} ,
2022-11-03 11:42:19 -04:00
"autoscaler" : map [ string ] any {
2022-11-21 04:35:40 -05:00
"csp" : csp . String ( ) ,
2022-11-03 11:42:19 -04:00
"image" : i . autoscalerImage ,
} ,
2022-11-21 11:06:41 -05:00
"verification-service" : map [ string ] any {
"csp" : csp . String ( ) ,
"image" : i . verificationServiceImage ,
} ,
2022-10-21 06:01:28 -04:00
}
2022-10-26 04:37:10 -04:00
switch csp {
case cloudprovider . Azure :
2022-11-21 04:35:40 -05:00
joinServiceVals , ok := values [ "join-service" ] . ( map [ string ] any )
2022-11-18 04:05:02 -05:00
if ! ok {
2022-11-21 04:35:40 -05:00
return nil , nil , errors . New ( "invalid join-service values" )
2022-11-18 04:05:02 -05:00
}
joinServiceVals [ "enforceIdKeyDigest" ] = config . EnforcesIDKeyDigest ( )
2022-11-21 04:35:40 -05:00
ccmVals , ok := values [ "ccm" ] . ( map [ string ] any )
2022-11-18 04:05:02 -05:00
if ! ok {
2022-11-21 04:35:40 -05:00
return nil , nil , errors . New ( "invalid ccm values" )
2022-11-18 04:05:02 -05:00
}
ccmVals [ "Azure" ] = map [ string ] any {
"image" : i . ccmImage ,
}
2022-11-21 04:35:40 -05:00
values [ "cnm" ] = map [ string ] any {
2022-11-18 04:05:02 -05:00
"image" : i . cnmImage ,
}
2022-11-21 04:35:40 -05:00
values [ "azure" ] = map [ string ] any {
2022-11-18 04:05:02 -05:00
"deployCSIDriver" : config . DeployCSIDriver ( ) ,
}
2022-11-21 04:35:40 -05:00
values [ "azuredisk-csi-driver" ] = map [ string ] any {
2022-11-18 09:47:01 -05:00
"node" : map [ string ] any {
"kmsPort" : constants . KMSPort ,
"kmsNamespace" : "" , // empty namespace means we use the release namespace
} ,
}
2022-11-21 04:35:40 -05:00
values [ "tags" ] = map [ string ] any {
2022-11-18 04:05:02 -05:00
"Azure" : true ,
2022-10-26 04:37:10 -04:00
}
2022-11-18 04:05:02 -05:00
2022-10-26 04:37:10 -04:00
case cloudprovider . GCP :
2022-11-21 04:35:40 -05:00
ccmVals , ok := values [ "ccm" ] . ( map [ string ] any )
2022-11-18 04:05:02 -05:00
if ! ok {
2022-11-21 04:35:40 -05:00
return nil , nil , errors . New ( "invalid ccm values" )
2022-11-18 04:05:02 -05:00
}
ccmVals [ "GCP" ] = map [ string ] any {
"image" : i . ccmImage ,
}
2022-11-21 04:35:40 -05:00
values [ "gcp" ] = map [ string ] any {
2022-11-18 04:05:02 -05:00
"deployCSIDriver" : config . DeployCSIDriver ( ) ,
2022-10-26 04:37:10 -04:00
}
2022-11-18 04:05:02 -05:00
2022-11-21 04:35:40 -05:00
values [ "gcp-compute-persistent-disk-csi-driver" ] = map [ string ] any {
2022-11-18 04:05:02 -05:00
"csiNode" : map [ string ] any {
"kmsPort" : constants . KMSPort ,
"kmsNamespace" : "" , // empty namespace means we use the release namespace
} ,
}
2022-11-21 04:35:40 -05:00
values [ "tags" ] = map [ string ] any {
2022-11-18 04:05:02 -05:00
"GCP" : true ,
}
2022-10-26 04:37:10 -04:00
case cloudprovider . QEMU :
2022-11-21 04:35:40 -05:00
values [ "tags" ] = map [ string ] interface { } {
2022-11-18 04:05:02 -05:00
"QEMU" : true ,
2022-10-26 04:37:10 -04:00
}
2022-11-18 04:05:02 -05:00
2022-10-26 04:37:10 -04:00
case cloudprovider . AWS :
2022-11-21 04:35:40 -05:00
ccmVals , ok := values [ "ccm" ] . ( map [ string ] any )
2022-10-25 10:29:28 -04:00
if ! ok {
2022-11-21 04:35:40 -05:00
return nil , nil , errors . New ( "invalid ccm values" )
2022-10-26 04:37:10 -04:00
}
ccmVals [ "AWS" ] = map [ string ] any {
2022-10-31 14:25:02 -04:00
"image" : i . ccmImage ,
2022-10-26 04:37:10 -04:00
}
2022-11-21 04:35:40 -05:00
values [ "tags" ] = map [ string ] any {
2022-10-26 04:37:10 -04:00
"AWS" : true ,
2022-10-25 10:29:28 -04:00
}
2022-10-24 06:23:18 -04:00
}
2022-11-21 04:35:40 -05:00
return chart , values , nil
2022-10-21 06:01:28 -04:00
}
// marshalChart takes a Chart object, packages it to a temporary file and returns the content of that file.
// We currently need to take this approach of marshaling as dependencies are not marshaled correctly with json.Marshal.
// This stems from the fact that chart.Chart does not export the dependencies property.
func ( i * ChartLoader ) marshalChart ( chart * chart . Chart ) ( [ ] byte , error ) {
2022-10-31 14:25:02 -04:00
// A separate tmpdir path is necessary since during unit testing multiple go routines are accessing the same path, possibly deleting files for other routines.
tmpDirPath , err := os . MkdirTemp ( "" , "*" )
defer os . Remove ( tmpDirPath )
if err != nil {
return nil , fmt . Errorf ( "creating tmp dir: %w" , err )
}
path , err := chartutil . Save ( chart , tmpDirPath )
2022-10-21 06:01:28 -04:00
defer os . Remove ( path )
if err != nil {
2022-10-31 14:25:02 -04:00
return nil , fmt . Errorf ( "chartutil save: %w" , err )
2022-10-21 06:01:28 -04:00
}
chartRaw , err := os . ReadFile ( path )
if err != nil {
return nil , fmt . Errorf ( "reading packaged chart: %w" , err )
}
return chartRaw , nil
2022-08-12 04:20:19 -04:00
}
// taken from loader.LoadDir from the helm go module
// loadChartsDir loads from a directory.
//
// This loads charts only from directories.
func loadChartsDir ( efs embed . FS , dir string ) ( * chart . Chart , error ) {
utf8bom := [ ] byte { 0xEF , 0xBB , 0xBF }
// Just used for errors.
c := & chart . Chart { }
rules := ignore . Empty ( )
ifile , err := efs . ReadFile ( filepath . Join ( dir , ignore . HelmIgnore ) )
if err == nil {
r , err := ignore . Parse ( bytes . NewReader ( ifile ) )
if err != nil {
return c , err
}
rules = r
}
rules . AddDefaults ( )
files := [ ] * loader . BufferedFile { }
walk := func ( path string , d fs . DirEntry , err error ) error {
n := strings . TrimPrefix ( path , dir )
if n == "" {
// No need to process top level. Avoid bug with helmignore .* matching
// empty names. See issue https://github.com/kubernetes/helm/issues/1776.
return nil
}
// Normalize to / since it will also work on Windows
n = filepath . ToSlash ( n )
// Check input err
if err != nil {
return err
}
fi , err := d . Info ( )
if err != nil {
return err
}
if d . IsDir ( ) {
// Directory-based ignore rules should involve skipping the entire
// contents of that directory.
if rules . Ignore ( n , fi ) {
return filepath . SkipDir
}
return nil
}
// If a .helmignore file matches, skip this file.
if rules . Ignore ( n , fi ) {
return nil
}
// Irregular files include devices, sockets, and other uses of files that
// are not regular files. In Go they have a file mode type bit set.
// See https://golang.org/pkg/os/#FileMode for examples.
if ! fi . Mode ( ) . IsRegular ( ) {
return fmt . Errorf ( "cannot load irregular file %s as it has file mode type bits set" , path )
}
data , err := efs . ReadFile ( path )
if err != nil {
return errors . Wrapf ( err , "error reading %s" , n )
}
data = bytes . TrimPrefix ( data , utf8bom )
n = strings . TrimPrefix ( n , "/" )
files = append ( files , & loader . BufferedFile { Name : n , Data : data } )
return nil
}
if err := fs . WalkDir ( efs , dir , walk ) ; err != nil {
return c , err
}
return loader . LoadFiles ( files )
}