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

[wip] Add maintenance mode policy #561

Closed
wants to merge 2 commits into from

Conversation

alanmoran
Copy link

@alanmoran alanmoran commented Jan 23, 2018

First attempt at a PR to apicast. Really basic policy just to introduce myself to Lua and writing some tests.
Wasn't sure what was the best way to test my request didn't hit upstream.

Any comments, tips or suggestions?

@@ -0,0 +1,25 @@
#Maintenance Mode
Copy link
Contributor

Choose a reason for hiding this comment

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

Github fails to render this correctly. You need to add a space after '#'.

Copy link
Contributor

Choose a reason for hiding this comment

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

Same with all the other titles in this doc.


##Example Configuration
```json
"policy_chain": [{
Copy link
Contributor

Choose a reason for hiding this comment

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

I would wrap this between { and } so it becomes a valid json. Also, I think that indenting this would help readability.



function _M.new(configuration)
local policy = new(configuration)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's no need to pass configuration as a param here.

Copy link
Contributor

Choose a reason for hiding this comment

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

This made me realize that we are doing the same in the echo policy. We'll need to fix that.

Copy link
Contributor

Choose a reason for hiding this comment

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

@davidor I think we might want to keep this as this is the signature of the initializer. That the underlying policy does not do anything with it is an implementation detail that can change in the future. What if the policy would actually store it in self.configuration ? We would have to change all policies to pass it again.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mikz I see what you mean, but new() here is calling function policy.new() in policy.lua which does not accept any parameters. Maybe we should add it then to make it less confusing?

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess we should document it and accept _configuration parameter which would not be used.

Copy link
Author

Choose a reason for hiding this comment

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

@davidor Ah damn. I actually followed the echo as my baseline. Are you both happy for me to keep this as the signature or change it for now?


function _M:access()

local status = self.status_code
Copy link
Contributor

Choose a reason for hiding this comment

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

These 2 lines are not properly indented.

return ngx.exit(ngx.status)
end

return _M
Copy link
Contributor

Choose a reason for hiding this comment

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

Github is complaining because there isn't a newline here.
Same with the rest of files included in this PR.

Copy link
Author

Choose a reason for hiding this comment

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

Mmm is this still complaining? I'm fairly sure my git removes the last newline of a file. (I need to check this)

Copy link
Member

Choose a reason for hiding this comment

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

@alanmoran I think there needs to be a new line.

Copy link
Author

Choose a reason for hiding this comment

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

@kevprice83 I added it and its there locally but I think git or github removing it.

"proxy": {
"policy_chain": [
{ "name": "apicast.policy.maintenance_mode",
"configuration": {
Copy link
Contributor

Choose a reason for hiding this comment

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

Check indentation here.

Testing 3 things:
1) Check default status code
2) Check default response message
3) TODO - validate upstream doesn't get the request
Copy link
Contributor

Choose a reason for hiding this comment

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

There are several options. You could print something in upstream and then check that it does not appear in the output, or you could put and assertion using luassert.

@davidor
Copy link
Contributor

davidor commented Jan 23, 2018

Great job @alanmoran 👍

I added some comments about minor issues I found, but this is a good example of how to write and test a policy. I would be interested to hear if you found the documentation to be good enough and the problems you had while writing the policy.

I don't think we should include this in the gateway/src/apicast/policy, but rather in the examples folder.

@alanmoran alanmoran force-pushed the add_maintenance_policy branch 2 times, most recently from 04440d0 to e38cfa4 Compare January 24, 2018 04:48
@alanmoran
Copy link
Author

@davidor Thanks for the review. I really appreciate it. I've made most of the changes you've recommended and put my feedback into @vramosp feedback doc.

Agree on not adding it as one of the default OOTB polices. I'll wait until I hear back about where community policies might live. Could be a good first candidate.

@andrewdavidmackenzie
Copy link
Member

consider adding as an example in a sub-folder for them?

Copy link
Contributor

@mikz mikz left a comment

Choose a reason for hiding this comment

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

Would be good to rebase this PR as #581 added an option to add policies to the example folder. And #579 changed the structure so the main file is called init.lua.
That should be the final structure of policies. Sorry for the hassle.

Guess we will have to figure out how to write and where to store the tests. I'd probably go for the policy itself or the examples/policies/t for tests using several of them. But that will require some changes to our CI and the test runner. I'll make those changes soon.

@alanmoran alanmoran force-pushed the add_maintenance_policy branch 2 times, most recently from 0fdb184 to ca64a78 Compare February 20, 2018 01:35
@alanmoran
Copy link
Author

alanmoran commented Feb 20, 2018

@mikz I've rebased and moved policy to the examples folder. Updated structure to use init.lua also.

You should be able to test by updating examples/policies/example.json:

{
  "services": [
    {
      "proxy": {
        "policy_chain": [
          {"name": "maintenance-mode", "version": "1.0.0",
            "configuration": {"message": "Be back soon..", "status": 503} },
          { "name": "ngx-example", "version": "1.0.0",
            "configuration": { "set_header": [{"name": "Example", "value": "Value" }] } },
          { "name": "apicast.policy.upstream",
            "configuration": {
              "rules": [{
                "regex": "/",
                "url": "http://echo:8081"
              }]
            }
          }
        ]
      }
    }
  ]
}

I haven't moved the tests yet but will do once we've decided what to do with them.

@alanmoran alanmoran force-pushed the add_maintenance_policy branch from ca64a78 to 5278df3 Compare February 20, 2018 01:55
@davidor
Copy link
Contributor

davidor commented Feb 20, 2018

@alanmoran good job 👍

Unfortunately, as you can see, the tests failed on CircleCI.

nginx: [error] init_by_lua error: module 'init' not found:
no file '/opt/app-root/src/gateway/policies/maintenance_mode/builtin/init.lua'
no file '/opt/app-root/src/gateway/src/apicast/policy/maintenance_mode/init.lua'

This is because by default, Apicast does not look for policies in the examples directory where your policy is. We need to configure Apicast to look in that directory. This can be done using the APICAST_POLICY_LOAD_PATH https://github.com/3scale/apicast/blob/master/doc/parameters.md#apicast_policy_load_path or the --policy-load-path option. In the tests, this is the way we pass ENVs:

env_to_apicast(
    'APICAST_POLICY_LOAD_PATH' => "$ENV{PWD}/examples/policies"
);

That needs to be specified before run_tests(). You can find several examples in our existing tests.

There's another problem. All the policies that are not the built-in ones, need to have a version in the configuration so Apicast knows how to load them. So you need something like this in the configuration:

{
    "name": "maintenance-mode", "version": "1.0.0"
}

Also, notice that you specified maintenance-mode, but the path is called maintenance_mode. They need to match.

Hope this helps. If anything is not clear or you need help please let me know.

@alanmoran alanmoran force-pushed the add_maintenance_policy branch 6 times, most recently from 90c906f to d0fcdc3 Compare February 20, 2018 23:43
@alanmoran alanmoran force-pushed the add_maintenance_policy branch 2 times, most recently from 7109d0e to 8546b16 Compare February 28, 2018 05:54
@alanmoran
Copy link
Author

@davidor I've fixed up the PR. Biggest change really is that I moved the tests under examples/policies/t. I believe we need to do further work to get the CI testing those.

In terms of maintenance-mode and maintenance_mode, I actually followed ngx-example but happy to change. Doesn't seem to break?

@mikz
Copy link
Contributor

mikz commented Feb 28, 2018

@alanmoran after #632 is merged CI will run integration tests in examples folder too.

"backend_authentication_value": "token-value",
"proxy": {
"policy_chain": [{
"name": "maintenance-mode", "version": "1.0.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is invalid JSON and the test fails. There is missing , at the and of the line.

return policy
end

function _M:access()
Copy link
Contributor

Choose a reason for hiding this comment

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

Both tests fail because this is supposed to be rewrite.

APIcast policy rejects the requests in rewrite phase because they don't have authentication.
So you either want to first require authentication and then show a maintenance page or show maintenance regardless of passed authentication parameters.

@davidor
Copy link
Contributor

davidor commented Aug 6, 2019

@alanmoran There's a new requirement to include this policy as a builtin one. I used your commits in #1105

@davidor davidor closed this Aug 6, 2019
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.

5 participants