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

Create check to ensure PDB have MaxUnavailable and MinAvailable set effectively #507

Merged
merged 3 commits into from Apr 7, 2023

Conversation

fahlmant
Copy link
Contributor

@fahlmant fahlmant commented Mar 7, 2023

Creates two checks for PDBs:

  • Recommends changes to PDBs that have MaxUnavailable set to 0
  • Recommends changes to PDBs that have MinAvailable set to the Replica value from related Deployments

This is a WIP. Tests still need to be added

docs/generated/checks.md Outdated Show resolved Hide resolved
docs/generated/checks.md Outdated Show resolved Hide resolved
pkg/builtinchecks/yamls/pdbs.yaml Outdated Show resolved Hide resolved
pkg/templates/pdb/template.go Outdated Show resolved Hide resolved
pkg/templates/pdb/template.go Outdated Show resolved Hide resolved
pkg/templates/pdb/template.go Outdated Show resolved Hide resolved
pkg/templates/pdb/template.go Outdated Show resolved Hide resolved
pkg/templates/pdb/template.go Outdated Show resolved Hide resolved
@tremes
Copy link
Contributor

tremes commented Mar 15, 2023

It's still not clear to me when and how this new check can be enabled. It seems it won't be enabled by default so will it be a custom check?

@fahlmant
Copy link
Contributor Author

@tremes the PR is still a WIP

@fahlmant
Copy link
Contributor Author

Tests currently are not working, adding a Deployment to the lintContext seems to not be working as the mocked object is showing that there's no Kind specified

@fahlmant fahlmant changed the title [WIP] Create check to ensure PDB have MaxUnavailable set effectively [WIP] Create check to ensure PDB have MaxUnavailable and MinAvailable set effectively Apr 3, 2023
@fahlmant fahlmant force-pushed the pdb-deployment-check branch 2 times, most recently from 93fc9da to 6d6cc89 Compare April 3, 2023 21:04
@fahlmant fahlmant changed the title [WIP] Create check to ensure PDB have MaxUnavailable and MinAvailable set effectively Create check to ensure PDB have MaxUnavailable and MinAvailable set effectively Apr 3, 2023
@fahlmant fahlmant force-pushed the pdb-deployment-check branch 2 times, most recently from fd7cbfa to 0f2dfd2 Compare April 3, 2023 21:24
pkg/lintcontext/mocks/pod.go Outdated Show resolved Hide resolved
replicas, _ := extract.Replicas(dl)
if isPercent {
// Calulate the actual value of the MinAvailable with respect to the Replica count if a percentage is set
pdbMinAvailable = int(replicas) * (value / 100)
Copy link
Contributor

Choose a reason for hiding this comment

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

Won't it be always zero? I guess you need something like pdbMinAvailable = float64(replicas) * (float64(value) / float64(100))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the end this still has to be an integer. It doesn't matter too much though because as long as it's less than 100%, it should be a valid config. For accuracy though, I think this will work:
pdbMinAvailable = int(math.Floor(float64(replicas) * (float64(value) / float64(100))))

This finds the exact value of minavailable based on a percentage, and then rounds down so cases like 1 replica with a 50% minavaiable (which calculates to 0.5) won't cause this remediation to fire. I'm not sure exactly how kubernetes handles this sort of case, so I'm erring on the side of permissive at first. It might be a good place for iteration

Copy link
Collaborator

Choose a reason for hiding this comment

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

@tremes is right but if we change order (multiplication first) it will work without conversion and floor

pdbMinAvailable = (int(replicas) * value) / 100

e2etests/bats-tests.sh Outdated Show resolved Hide resolved
docs/generated/checks.md Outdated Show resolved Hide resolved
if err != nil {
return []diagnostic.Diagnostic{
{
Message: fmt.Sprintf("pdb has invalid MinAvailable value: %v", err),
Copy link
Collaborator

Choose a reason for hiding this comment

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

in other places PDB is capital.

Suggested change
Message: fmt.Sprintf("pdb has invalid MinAvailable value: %v", err),
Message: fmt.Sprintf("PDB has invalid MinAvailable value: %w", err),

Copy link
Collaborator

Choose a reason for hiding this comment

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

also in other places we use errors.Wrap

pkg/templates/pdbminavailable/template.go Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
@janisz janisz self-requested a review April 5, 2023 14:35
Copy link
Contributor

@dhaus67 dhaus67 left a comment

Choose a reason for hiding this comment

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

Just minor things, otherwise LGTM!

docs/generated/checks.md Outdated Show resolved Hide resolved
tests/checks/pdb-min-available.yaml Outdated Show resolved Hide resolved
Copy link
Collaborator

@janisz janisz left a comment

Choose a reason for hiding this comment

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

Just small comments. I think we should use errors.Wrap to wrap errors.

pkg/templates/pdbminavailable/template.go Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
replicas, _ := extract.Replicas(dl)
if isPercent {
// Calulate the actual value of the MinAvailable with respect to the Replica count if a percentage is set
pdbMinAvailable = int(replicas) * (value / 100)
Copy link
Collaborator

Choose a reason for hiding this comment

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

@tremes is right but if we change order (multiplication first) it will work without conversion and floor

pdbMinAvailable = (int(replicas) * value) / 100

pkg/templates/pdbminavailable/template.go Outdated Show resolved Hide resolved
replicas: 1
selector:
matchLabels:
name: cloud-ingress-operator
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should add a check for EOL

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you clarify this a bit? I'm not sure what is being asked here

@janisz janisz self-requested a review April 7, 2023 15:13
@janisz janisz merged commit e94f8f3 into stackrox:main Apr 7, 2023
7 checks passed
@maorfr
Copy link

maorfr commented Sep 11, 2023

abrad3 pushed a commit to abrad3/kube-linter that referenced this pull request Sep 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants