Skip to content

Commit

Permalink
Add debugging logs for repository resource config (#646)
Browse files Browse the repository at this point in the history
* Add debugging logs for repository resource config

* Remove function name from debugging logs of contentsMatch
  • Loading branch information
MahmoudOuka authored Jul 23, 2024
1 parent bd75fc2 commit e54f784
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion config/file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (f *fileResource) checkState(ctx context.Context) (inDesiredState bool, err
case agentendpointpb.OSPolicy_Resource_FileResource_PRESENT:
return util.Exists(f.managedFile.Path), nil
case agentendpointpb.OSPolicy_Resource_FileResource_CONTENTS_MATCH:
return contentsMatch(f.managedFile.Path, f.managedFile.checksum)
return contentsMatch(ctx, f.managedFile.Path, f.managedFile.checksum)
default:
return false, fmt.Errorf("unrecognized DesiredState for FileResource: %q", f.managedFile.State)
}
Expand Down
2 changes: 1 addition & 1 deletion config/file_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ func TestFileResourceEnforceStatePresent(t *testing.T) {
t.Fatalf("Unexpected EnforceState error: %v", err)
}

match, err := contentsMatch(wantFile, pr.resource.(*fileResource).managedFile.checksum)
match, err := contentsMatch(ctx, wantFile, pr.resource.(*fileResource).managedFile.checksum)
if err != nil {
t.Fatal(err)
}
Expand Down
16 changes: 12 additions & 4 deletions config/repository_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,23 +279,31 @@ func (r *repositoryResource) validate(ctx context.Context) (*ManagedResources, e
return &ManagedResources{Repositories: []ManagedRepository{r.managedRepository}}, nil
}

func contentsMatch(path, chksum string) (bool, error) {
func contentsMatch(ctx context.Context, path, chksum string) (bool, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
if os.IsNotExist(err) {
clog.Debugf(ctx, "File not found: %s", path)
return false, nil
}
clog.Debugf(ctx, "Error opening file %s.", path)
return false, err
}
defer file.Close()

return chksum == checksum(file), nil
actualChecksum := checksum(file)
if actualChecksum != chksum {
clog.Debugf(ctx, "Checksums don't match, got: %s, actual: %s", chksum, actualChecksum)
return false, nil
}

return true, nil
}

func (r *repositoryResource) checkState(ctx context.Context) (inDesiredState bool, err error) {
// Check APT gpg key if applicable.
if r.managedRepository.Apt != nil && r.managedRepository.Apt.GpgFileContents != nil {
match, err := contentsMatch(r.managedRepository.Apt.GpgFilePath, r.managedRepository.Apt.GpgChecksum)
match, err := contentsMatch(ctx, r.managedRepository.Apt.GpgFilePath, r.managedRepository.Apt.GpgChecksum)
if err != nil {
return false, err
}
Expand All @@ -304,7 +312,7 @@ func (r *repositoryResource) checkState(ctx context.Context) (inDesiredState boo
}
}

return contentsMatch(r.managedRepository.RepoFilePath, r.managedRepository.RepoChecksum)
return contentsMatch(ctx, r.managedRepository.RepoFilePath, r.managedRepository.RepoChecksum)
}

func (r *repositoryResource) enforceState(ctx context.Context) (inDesiredState bool, err error) {
Expand Down
2 changes: 1 addition & 1 deletion config/repository_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func TestRepositoryResourceEnforceState(t *testing.T) {
t.Fatalf("Unexpected EnforceState error: %v", err)
}

match, err := contentsMatch(pr.resource.(*repositoryResource).managedRepository.RepoFilePath, pr.resource.(*repositoryResource).managedRepository.RepoChecksum)
match, err := contentsMatch(ctx, pr.resource.(*repositoryResource).managedRepository.RepoFilePath, pr.resource.(*repositoryResource).managedRepository.RepoChecksum)
if err != nil {
t.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions packages/zypper.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func ZypperListPatchAll(all bool) ZypperListOption {

// InstallZypperPackages Installs zypper packages
func InstallZypperPackages(ctx context.Context, pkgs []string) error {
// TODO: Add retries when the it fails with exit code: 7 - which means zypper is locked by another process id.
_, err := run(ctx, zypper, append(zypperInstallArgs, pkgs...))
return err
}
Expand Down

0 comments on commit e54f784

Please sign in to comment.