knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/configmap/load.go (about)

     1  /*
     2  Copyright 2018 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package configmap
    18  
    19  import (
    20  	"os"
    21  	"path"
    22  	"path/filepath"
    23  )
    24  
    25  // Load reads the "Data" of a ConfigMap from a particular VolumeMount.
    26  func Load(p string) (map[string]string, error) {
    27  	data := make(map[string]string)
    28  	err := filepath.Walk(p, func(p string, info os.FileInfo, err error) error {
    29  		if err != nil {
    30  			return err
    31  		}
    32  		for info.Mode()&os.ModeSymlink != 0 {
    33  			dirname := filepath.Dir(p)
    34  			p, err = os.Readlink(p)
    35  			if err != nil {
    36  				return err
    37  			}
    38  			if !filepath.IsAbs(p) {
    39  				p = path.Join(dirname, p)
    40  			}
    41  			info, err = os.Lstat(p)
    42  			if err != nil {
    43  				return err
    44  			}
    45  		}
    46  		if info.IsDir() {
    47  			return nil
    48  		}
    49  		b, err := os.ReadFile(p)
    50  		if err != nil {
    51  			return err
    52  		}
    53  		data[info.Name()] = string(b)
    54  		return nil
    55  	})
    56  	return data, err
    57  }