Skip to content
This repository was archived by the owner on Aug 14, 2020. It is now read-only.

Commit ae17631

Browse files
author
Yifan Gu
committed
spec: add ExitPolicy type in pod manifest.
The optional `ExitPolicy` type defines the behavior of the pod when the apps within it exit. This PR adds 3 valid policies: - untilAll: The pod exits only when all the apps exit (no matter they are successful or not). - onAny: The pod exits when any of the apps exit (no matter they are successful or not). - onAnyFailure: The pod exits when any of the apps exit unsuccessfully.
1 parent 0d75d36 commit ae17631

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

examples/pod_runtime.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@
102102
"name": "ip-address",
103103
"value": "10.1.2.3"
104104
}
105-
]
105+
],
106+
"exitPolicy": "onAnyFailure"
106107
}

schema/pod.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type PodManifest struct {
3232
Isolators []types.Isolator `json:"isolators"`
3333
Annotations types.Annotations `json:"annotations"`
3434
Ports []types.ExposedPort `json:"ports"`
35+
ExitPolicy types.ExitPolicy `json:"exitPolicy"`
3536
}
3637

3738
// podManifest is a model to facilitate extra validation during the

schema/types/exitpolicy.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2015 The appc Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package types
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
)
21+
22+
type ExitPolicy string
23+
24+
var validPolicies = map[ExitPolicy]struct{}{
25+
"untilAll": struct{}{},
26+
"onAny": struct{}{},
27+
"onAnyFailure": struct{}{},
28+
}
29+
30+
type exitPolicy ExitPolicy
31+
32+
func (e *ExitPolicy) UnmarshalJSON(data []byte) error {
33+
var ep exitPolicy
34+
if err := json.Unmarshal(data, &ep); err != nil {
35+
return err
36+
}
37+
ee := ExitPolicy(ep)
38+
if err := ee.assertValid(); err != nil {
39+
return err
40+
}
41+
*e = ee
42+
return nil
43+
}
44+
45+
func (e ExitPolicy) MarshalJSON() ([]byte, error) {
46+
if err := e.assertValid(); err != nil {
47+
return nil, err
48+
}
49+
return json.Marshal(exitPolicy(e))
50+
}
51+
52+
func (e ExitPolicy) assertValid() error {
53+
if _, ok := validPolicies[e]; !ok {
54+
return fmt.Errorf("invalid exit policy %q", string(e))
55+
}
56+
return nil
57+
}

schema/types/exitpolicy_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2015 The appc Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package types
16+
17+
import (
18+
"testing"
19+
)
20+
21+
func TestGoodExitPolicy(t *testing.T) {
22+
for e := range validPolicies {
23+
if err := e.assertValid(); err != nil {
24+
t.Errorf("good exit policy failed: %v", err)
25+
}
26+
}
27+
}
28+
29+
func TestBadExitPolicy(t *testing.T) {
30+
e := ExitPolicy("bad")
31+
if err := e.assertValid(); err == nil {
32+
t.Errorf("bad exit policy valid: %v", err)
33+
}
34+
}

spec/pods.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ JSON Schema for the Pod Manifest, conforming to [RFC4627](https://tools.ietf.org
150150
"name": "ftp",
151151
"hostPort": 2121
152152
}
153-
]
153+
],
154+
"exitPolicy": "onAnyFailure"
154155
}
155156
```
156157

@@ -179,3 +180,7 @@ JSON Schema for the Pod Manifest, conforming to [RFC4627](https://tools.ietf.org
179180
* **ports** (list of objects, optional) list of ports that SHOULD be exposed on the host.
180181
* **name** (string, required, restricted to the [AC Name](#ac-name-type) formatting) name of the port to be exposed on the host. This field is a key referencing by name ports specified in the Image Manifest(s) of the app(s) within this Pod Manifest; consequently, port names MUST be unique among apps within a pod.
181182
* **hostPort** (integer, required) port number on the host that will be mapped to the application port.
183+
* **exitPolicy** (string, optional) a string that specify the exit policy of the pod, if left empty, then it's up to ACE to choose the default behavior. Valid values are:
184+
* **untilAll** - the pod exits only when all the apps exit, no matter they are successful or not.
185+
* **onAny** - the pod exits when any of the apps exits either successfully or unsuccessfully.
186+
* **onAnyFailure** -the pod exits when any of the pod exits unsuccessfully, also the pod exits when there is no app running.

0 commit comments

Comments
 (0)