github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/tag/input_digest.go (about)

     1  /*
     2  Copyright 2019 The Skaffold 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 tag
    18  
    19  import (
    20  	"context"
    21  	"crypto/md5"
    22  	"crypto/sha256"
    23  	"encoding/hex"
    24  	"encoding/json"
    25  	"fmt"
    26  	"io"
    27  	"os"
    28  	"path/filepath"
    29  	"sort"
    30  
    31  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    32  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    33  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output/log"
    34  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    35  )
    36  
    37  type inputDigestTagger struct {
    38  	cfg   docker.Config
    39  	cache graph.SourceDependenciesCache
    40  }
    41  
    42  func NewInputDigestTagger(cfg docker.Config, ag graph.ArtifactGraph) (Tagger, error) {
    43  	return &inputDigestTagger{
    44  		cfg:   cfg,
    45  		cache: graph.NewSourceDependenciesCache(cfg, nil, ag),
    46  	}, nil
    47  }
    48  
    49  func (t *inputDigestTagger) GenerateTag(ctx context.Context, image latest.Artifact) (string, error) {
    50  	var inputs []string
    51  	srcFiles, err := t.cache.TransitiveArtifactDependencies(ctx, &image)
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  
    56  	// must sort as hashing is sensitive to the order in which files are processed
    57  	sort.Strings(srcFiles)
    58  	for _, d := range srcFiles {
    59  		h, err := fileHasher(d, image.Workspace)
    60  		if err != nil {
    61  			if os.IsNotExist(err) {
    62  				log.Entry(ctx).Tracef("skipping dependency %q for artifact cache calculation: %v", d, err)
    63  				continue // Ignore files that don't exist
    64  			}
    65  
    66  			return "", fmt.Errorf("getting hash for %q: %w", d, err)
    67  		}
    68  		inputs = append(inputs, h)
    69  	}
    70  
    71  	return encode(inputs)
    72  }
    73  
    74  func encode(inputs []string) (string, error) {
    75  	// get a key for the hashes
    76  	hasher := sha256.New()
    77  	enc := json.NewEncoder(hasher)
    78  	if err := enc.Encode(inputs); err != nil {
    79  		return "", err
    80  	}
    81  	return hex.EncodeToString(hasher.Sum(nil)), nil
    82  }
    83  
    84  // fileHasher hashes the contents and name of a file
    85  func fileHasher(path string, workspacePath string) (string, error) {
    86  	h := md5.New()
    87  	fi, err := os.Lstat(path)
    88  	if err != nil {
    89  		return "", err
    90  	}
    91  	// Always try to use the file path relative to workspace when calculating hash.
    92  	// This will ensure we will always get the same hash independent of workspace location and hierarchy.
    93  	pathToHash, err := filepath.Rel(workspacePath, path)
    94  	if err != nil {
    95  		pathToHash = path
    96  	}
    97  	h.Write([]byte(pathToHash))
    98  
    99  	if fi.Mode().IsRegular() {
   100  		f, err := os.Open(path)
   101  		if err != nil {
   102  			return "", err
   103  		}
   104  		defer f.Close()
   105  		if _, err := io.Copy(h, f); err != nil {
   106  			return "", err
   107  		}
   108  	}
   109  	return hex.EncodeToString(h.Sum(nil)), nil
   110  }