Skip to content

Commit

Permalink
test with writing a file approach
Browse files Browse the repository at this point in the history
Signed-off-by: ashnamehrotra <[email protected]>
  • Loading branch information
ashnamehrotra committed Jun 20, 2024
1 parent 35da7f7 commit ad5b85f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
16 changes: 16 additions & 0 deletions pkg/buildkit/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ func InitializeBuildkitConfig(ctx context.Context, c gwclient.Client, image stri
return &config, nil
}

func FetchRef(client gwclient.Client, st llb.State, ctx context.Context, opts ...llb.ConstraintsOpt) (*gwclient.Result, error) {

Check failure on line 62 in pkg/buildkit/buildkit.go

View workflow job for this annotation

GitHub Actions / lint

hugeParam: st is heavy (80 bytes); consider passing it by pointer (gocritic)
def, err := st.Marshal(ctx, opts...)
if err != nil {
return nil, err

Check warning on line 65 in pkg/buildkit/buildkit.go

View check run for this annotation

Codecov / codecov/patch

pkg/buildkit/buildkit.go#L62-L65

Added lines #L62 - L65 were not covered by tests
}

res, err := client.Solve(ctx, gwclient.SolveRequest{
Definition: def.ToPB(),
})
if err != nil {
return nil, err

Check warning on line 72 in pkg/buildkit/buildkit.go

View check run for this annotation

Codecov / codecov/patch

pkg/buildkit/buildkit.go#L68-L72

Added lines #L68 - L72 were not covered by tests
}

return res, nil

Check warning on line 75 in pkg/buildkit/buildkit.go

View check run for this annotation

Codecov / codecov/patch

pkg/buildkit/buildkit.go#L75

Added line #L75 was not covered by tests
}

// Extracts the bytes of the file denoted by `path` from the state `st`.
func ExtractFileFromState(ctx context.Context, c gwclient.Client, st *llb.State, path string) ([]byte, error) {
// since platform is obtained from host, override it in the case of Darwin
Expand Down
43 changes: 32 additions & 11 deletions pkg/pkgmgr/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (rm *rpmManager) InstallUpdates(ctx context.Context, manifest *unversioned.
return nil, nil, err
}
} else {
updatedImageState, resultManifestBytes, err = rm.installUpdates(ctx, updates, toolImageName)
updatedImageState, resultManifestBytes, err = rm.installUpdates(ctx, updates)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -384,7 +384,7 @@ func parseManifestFile(file string) (map[string]string, error) {
//
// TODO: Support RPM-based images with valid rpm status but missing tools. (e.g. calico images > v3.21.0)
// i.e. extra RunOption to mount a copy of rpm tools installed into the image and invoking that.
func (rm *rpmManager) installUpdates(ctx context.Context, updates unversioned.UpdatePackages, toolingImage string) (*llb.State, []byte, error) {
func (rm *rpmManager) installUpdates(ctx context.Context, updates unversioned.UpdatePackages) (*llb.State, []byte, error) {
pkgs := ""

// If specific updates, provided, parse into pkg names, else will update all
Expand All @@ -397,24 +397,28 @@ func (rm *rpmManager) installUpdates(ctx context.Context, updates unversioned.Up
pkgs = strings.Join(pkgStrings, " ")
}

// Check for upgradable packages
// Mount filesystem of image to tooling image to run dnf check-update
tool := llb.Image(toolingImage, llb.ResolveModeDefault).Run(llb.Shlex(installToolsCmd), llb.WithProxy(utils.GetProxy()))
busyboxCopied := tool.Dir(downloadPath).Run(llb.Shlex("cp /usr/sbin/busybox .")).Root()
dnfCheckCmd := fmt.Sprintf("dnf install ca-certificates -y; dnf --refresh --installroot=/tmp/rootfs --releasever %s check-update -y; if [ $? -eq 0 ]; then exit 1; fi;", rm.osVersion)

rm.config.ImageState = busyboxCopied.Run(llb.Args([]string{"sh", "-c", dnfCheckCmd})).AddMount("/tmp/rootfs", rm.config.ImageState)

// Install patches using available rpm managers in order of preference
var installCmd string
switch {
case rm.rpmTools["dnf"] != "":
if rm.checkForUpgrades(ctx, rm.rpmTools["dnf"]) != nil {
return nil, nil, errors.New("no upgradable packages")

Check warning on line 405 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L404-L405

Added lines #L404 - L405 were not covered by tests
}

const dnfInstallTemplate = `sh -c '%[1]s upgrade %[2]s -y && %[1]s clean all'`
installCmd = fmt.Sprintf(dnfInstallTemplate, rm.rpmTools["dnf"], pkgs)
case rm.rpmTools["yum"] != "":
if rm.checkForUpgrades(ctx, rm.rpmTools["yum"]) != nil {
return nil, nil, errors.New("no upgradable packages")

Check warning on line 412 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L411-L412

Added lines #L411 - L412 were not covered by tests
}

const yumInstallTemplate = `sh -c '%[1]s upgrade %[2]s -y && %[1]s clean all'`
installCmd = fmt.Sprintf(yumInstallTemplate, rm.rpmTools["yum"], pkgs)
case rm.rpmTools["microdnf"] != "":
if rm.checkForUpgrades(ctx, rm.rpmTools["microdnf"]) != nil {
return nil, nil, errors.New("no upgradable packages")

Check warning on line 419 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L418-L419

Added lines #L418 - L419 were not covered by tests
}

const microdnfInstallTemplate = `sh -c '%[1]s update %[2]s && %[1]s clean all'`
installCmd = fmt.Sprintf(microdnfInstallTemplate, rm.rpmTools["microdnf"], pkgs)
default:
Expand All @@ -425,12 +429,12 @@ func (rm *rpmManager) installUpdates(ctx context.Context, updates unversioned.Up

// Write results.manifest to host for post-patch validation
var resultBytes []byte
var err error
if updates != nil {
const rpmResultsTemplate = `sh -c 'rpm -qa --queryformat "%s" %s > "%s"'`
outputResultsCmd := fmt.Sprintf(rpmResultsTemplate, resultQueryFormat, pkgs, resultManifest)
resultsWritten := installed.Dir(resultsPath).Run(llb.Shlex(outputResultsCmd)).AddMount(resultsPath, llb.Scratch())

var err error

Check warning on line 437 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L437

Added line #L437 was not covered by tests
resultBytes, err = buildkit.ExtractFileFromState(ctx, rm.config.Client, &resultsWritten, resultManifest)
if err != nil {
return nil, nil, err
Expand All @@ -443,6 +447,23 @@ func (rm *rpmManager) installUpdates(ctx context.Context, updates unversioned.Up
return &patchMerge, resultBytes, nil
}

func (rm *rpmManager) checkForUpgrades(ctx context.Context, toolPath string) error {
checkUpdateTemplate := `sh -c "%[1]s install dnf; dnf check-update; if [ $? -ne 0 ]; then echo >> updates.txt; fi;"`
checkUpdate := fmt.Sprintf(checkUpdateTemplate, toolPath)

Check warning on line 452 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L450-L452

Added lines #L450 - L452 were not covered by tests

stateWithDnf := rm.config.ImageState.Run(llb.Shlex(checkUpdate)).Root()

Check warning on line 454 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L454

Added line #L454 was not covered by tests

_, err := buildkit.ExtractFileFromState(ctx, rm.config.Client, &stateWithDnf, "updates.txt")

Check warning on line 456 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L456

Added line #L456 was not covered by tests

// if error in extracting file, that means updates.txt does not exist and there are no updates
if err != nil {
log.Error(err)
return err

Check warning on line 461 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L459-L461

Added lines #L459 - L461 were not covered by tests
}

return nil

Check warning on line 464 in pkg/pkgmgr/rpm.go

View check run for this annotation

Codecov / codecov/patch

pkg/pkgmgr/rpm.go#L464

Added line #L464 was not covered by tests
}

func (rm *rpmManager) unpackAndMergeUpdates(ctx context.Context, updates unversioned.UpdatePackages, toolImage string) (*llb.State, []byte, error) {
// Spin up a build tooling container to fetch and unpack packages to create patch layer.
// Pull family:version -> need to create version to base image map
Expand Down

0 comments on commit ad5b85f

Please sign in to comment.