Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding printing diff functionality in flux diff artifact #4138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 114 additions & 4 deletions cmd/flux/diff_artifact.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022 The Flux authors
Copyright 2023 The Flux authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,12 +17,18 @@ limitations under the License.
package main

import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"os"

oci "github.com/fluxcd/pkg/oci/client"
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
"github.com/google/go-containerregistry/pkg/crane"

"github.com/sergi/go-diff/diffmatchpatch"
"github.com/spf13/cobra"

"github.com/fluxcd/flux2/v2/internal/flags"
Expand All @@ -42,6 +48,7 @@ type diffArtifactFlags struct {
creds string
provider flags.SourceOCIProvider
ignorePaths []string
compareTo string // New field to capture the URL or local path for comparison
}

var diffArtifactArgs = newDiffArtifactArgs()
Expand All @@ -57,6 +64,7 @@ func init() {
diffArtifactCmd.Flags().StringVar(&diffArtifactArgs.creds, "creds", "", "credentials for OCI registry in the format <username>[:<password>] if --provider is generic")
diffArtifactCmd.Flags().Var(&diffArtifactArgs.provider, "provider", sourceOCIRepositoryArgs.provider.Description())
diffArtifactCmd.Flags().StringSliceVar(&diffArtifactArgs.ignorePaths, "ignore-paths", excludeOCI, "set paths to ignore in .gitignore format")
diffArtifactCmd.Flags().BoolP("verbose", "v", false, "Show verbose output") // Add a --verbose flag specific to diff artifact.
diffCmd.AddCommand(diffArtifactCmd)
}

Expand All @@ -67,7 +75,7 @@ func diffArtifactCmdRun(cmd *cobra.Command, args []string) error {
ociURL := args[0]

if diffArtifactArgs.path == "" {
return fmt.Errorf("invalid path %q", diffArtifactArgs.path)
return fmt.Errorf("path is required")
}

url, err := oci.ParseArtifactURL(ociURL)
Expand Down Expand Up @@ -103,10 +111,112 @@ func diffArtifactCmdRun(cmd *cobra.Command, args []string) error {
}
}

if err := ociClient.Diff(ctx, url, diffArtifactArgs.path, diffArtifactArgs.ignorePaths); err != nil {
ociContents, err := fetchContentsFromURL(ctx, url)
if err != nil {
return err
}

logger.Successf("no changes detected")
compareToContents, err := fetchContentsFromLocalPath(diffArtifactArgs.path)
if err != nil {
return err
}

diffOutput, err := diffContents(ociContents, compareToContents)
if err != nil {
return err
}

fmt.Printf("Diff between OCI artifact (%s) and local path (%s):\n", ociURL, diffArtifactArgs.path)

// Show verbose output if the --verbose flag is set
if verbose, _ := cmd.Flags().GetBool("verbose"); verbose {
fmt.Println("Printing diff...")
}

fmt.Println(diffOutput)

return nil
}

func fetchContentsFromURL(ctx context.Context, url string) ([]byte, error) {
img, err := crane.Pull(url)
if err != nil {
return nil, fmt.Errorf("failed to pull OCI artifact: %w", err)
}

layers, err := img.Layers()
if err != nil {
return nil, fmt.Errorf("failed to list layers: %w", err)
}

if len(layers) < 1 {
return nil, fmt.Errorf("no layers found in OCI artifact")
}

// Create a buffer to store the contents of the artifact
var contentsBuffer bytes.Buffer

// Extract the content of the first layer
layerContent, err := layers[0].Compressed()
if err != nil {
return nil, fmt.Errorf("failed to extract layer content: %w", err)
}
defer layerContent.Close()

// Use tar.NewReader to read the contents from the OCI artifact
tarReader := tar.NewReader(layerContent)
for {
_, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to read tar header: %w", err)
}

// Read the file contents and write them to the buffer
if _, err := io.Copy(&contentsBuffer, tarReader); err != nil {
return nil, fmt.Errorf("failed to read tar entry contents: %w", err)
}
}
Comment on lines +167 to +181
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of this looking at just file contents, I think it would be much better if path data would be taken into account (which also can be done for local directories, as they are expected to have the same structure as a decompressed tar).

This way, not only differences in file content would be observed. But also things being moved (or swapped) from file A -> B, files changing from path, etc.


return contentsBuffer.Bytes(), nil
}

func fetchContentsFromLocalPath(path string) ([]byte, error) {
// Open the local file or directory at the given path
file, err := os.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open local file/directory: %w", err)
}
defer file.Close()

// Create a buffer to store the contents
var contentsBuffer bytes.Buffer

// Use tar.NewReader to read the contents from the file/directory
tarReader := tar.NewReader(file)
for {
_, err := tarReader.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to read tar header: %w", err)
}

// Read the file contents and write them to the buffer
if _, err := io.Copy(&contentsBuffer, tarReader); err != nil {
return nil, fmt.Errorf("failed to read tar entry contents: %w", err)
}
}

return contentsBuffer.Bytes(), nil
}

func diffContents(contents1, contents2 []byte) (string, error) {
dmp := diffmatchpatch.New()

diffs := dmp.DiffMain(string(contents1), string(contents2), false)
return dmp.DiffPrettyText(diffs), nil
}