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

fix/linting for percentage based values #546

Merged
merged 1 commit into from
Apr 27, 2023
Merged

Conversation

bmiguel-teixeira
Copy link
Contributor

This PR fixes the PDB validation to include support for % based values.

OldBehavior:
maxUnavailable was set to 10% was failing since the field is IntOrString. The IntVal defaults to 0 when the field is of String Values, thus causing false positives.

NewBehaviour:
int and string values are taken into account, parsed into a single integer value for validation.

@janisz
Copy link
Collaborator

janisz commented Apr 17, 2023

@fahlmant PTAL

@jholecek-rh
Copy link

Since the MR is importing k8s.io/apimachinery/pkg/util/intstr, wouldn't it be better to refactor the code to use higher-level functions offered by that package, e.g. getIntOrPercentValueSafely?

@janisz janisz requested a review from dhaus67 April 18, 2023 14:06
Message: maxUnavailableZeroMsg,
},
if pdb.Spec.MaxUnavailable == nil {
if pdb.Spec.MaxUnavailable.IntVal == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the first condition is satisfied then the second one will fail with panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you

if pdb.Spec.MaxUnavailable == nil {
if pdb.Spec.MaxUnavailable.IntVal == 0 {
return []diagnostic.Diagnostic{{
Message: "MaxUnavailable is undefined",
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is "undesired" diagnostic. I mean we would like to report when MaxUnavailable is 0, otherwise do nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

still have a bit of mixed feeling on this one but a "missing" maxUnavailable is valid. skipping validation on nil. please take a look

Copy link
Contributor

Choose a reason for hiding this comment

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

yes it's valid. Thanks. Looks good.

@mklika
Copy link

mklika commented Apr 21, 2023

Hi @bmiguel-teixeira!

My name is Martin and I work at Red Hat along with Tomas. We're quite eager to see these changes merged as we bellieve PDB check could be quite helpful for our SRE organization.

Would you mind letting me know your estimate in terms of when would you have time look at comments from Tomas and fixing the code?

I'd like to also offer our help finishing this up should it be your preference due to other priorities you might have.

Many thanks in advance for letting me know

Martin Klika

@bmiguel-teixeira
Copy link
Contributor Author

Hi @mklika

Thanks for the heads up. Changes & fixes submitted. Please review

@tremes
Copy link
Contributor

tremes commented Apr 24, 2023

Looks good to me. Thank you @bmiguel-teixeira .

@@ -28,11 +28,49 @@ func (p *PDBTestSuite) SetupTest() {
p.ctx = mocks.NewMockContext()
}

func (p *PDBTestSuite) TestPDBMaxUnavailableZero() {
func (p *PDBTestSuite) TestPDBMaxUnavailableZeroAsInteger() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about converting this tests to table driven tests?

func (p *PDBTestSuite) TestPDB() {
	testCases := []struct {
		Name           string
		Message        string
		MaxUnavailable intstr.IntOrString
	}{
		{
			Name:           "Invalid Value String",
			Message:        "maxUnavailable has invalid value [invalid-value]",
			MaxUnavailable: intstr.FromString("invalid-value"),
		},
		{
			Name:           "Invalid Value String",
			Message:        "maxUnavailable has invalid value [0]",
			MaxUnavailable: intstr.FromString("0"),
		},
		{
			Name:           "One As String Percentage",
			MaxUnavailable: intstr.FromString("1%"),
		},
		{
			Name:           "One As Integer",
			MaxUnavailable: intstr.FromInt(1),
		},
		{
			Name:           "Zero As String Percentage",
			Message:        "MaxUnavailable is set to 0",
			MaxUnavailable: intstr.FromString("0%"),
		},
		{
			Name:           "Zero As Integer",
			Message:        "MaxUnavailable is set to 0",
			MaxUnavailable: intstr.FromInt(0),
		},
	}

	for _, tc := range testCases {
		tc := tc
		p.Run(tc.Name, func() {
			p.ctx.AddMockPodDisruptionBudget(p.T(), "test-pdb")
			p.ctx.ModifyPodDisruptionBudget(p.T(), "test-pdb", func(pdb *pdbV1.PodDisruptionBudget) {
				pdb.Spec.MaxUnavailable = &tc.MaxUnavailable
			})

			expected := map[string][]diagnostic.Diagnostic{}
			if tc.Message != "" {
				expected = map[string][]diagnostic.Diagnostic{
					"test-pdb": {{Message: tc.Message}},
				}
			}

			p.Validate(p.ctx, []templates.TestCase{
				{
					Param:                    params.Params{},
					Diagnostics:              expected,
					ExpectInstantiationError: false,
				},
			})
		})
	}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

make sense. done

@janisz janisz requested a review from tremes April 27, 2023 12:57
@janisz janisz merged commit 58b4948 into stackrox:main Apr 27, 2023
@janisz janisz added the bug Something isn't working label Apr 27, 2023
@ykadowak ykadowak mentioned this pull request May 8, 2023
4 tasks
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
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants