The Container case lets you define the settings per-container in an easy manner:
$container = K8s::container();
$container
->setName('mysql')
->setImage('mysql', '5.7')
->setPorts([
['name' => 'mysql', 'protocol' => 'TCP', 'containerPort' => 3306],
])
->addPort(3307, 'TCP', 'mysql-alt')
->setCommand(['mysqld'])
->setArgs(['--test'])
->setEnv(['MYSQL_ROOT_PASSWORD' => 'test']);
Later on, you can attach the Container
classes directly to the K8sPod
instance:
$pod = K8s::pod($cluster)
->setName('mysql')
->setSelectors(['app' => 'db'])
->setContainers([$mysql])
Please keep in mind that Containers does not have predefined functions, so you can extend the class or you can use Custom Callers, which applies to any Instance or Resource.
Pods support annotations, as well as labels:
$pod->setAnnotations([
'nginx.kubernetes.io/tls' => 'true',
]);
$pod->setLabels([
'matchesLabel' => ['app' => 'backend'],
]);
While the Pod kind has spec
, you can avoid writing this:
$pod = K8s::pod($cluster)
->setAttribute('spec.nodeSelector', [...]);
And use the setSpec()
method:
$pod = K8s::pod($cluster)
->setSpec('nodeSelector', [...]);
Dot notation is supported:
$pod = K8s::pod($cluster)
->setSpec('some.nested.path', [...]);
$pod = K8s::pod($cluster)
->whereName('mysql')
->get();
$containers = $pod->getContainers();
Retrieving the spec attributes can be done like the setSpec()
method:
$pod->getSpec('containers', []);
The second value for the getSpec()
method is the default value, in case the found path is not existent.
Dot notation is supported:
$pod->getSpec('some.nested.path', []);
Retrieving the containers and init containers can be retrieved as an array of \RenokiCo\PhpK8s\Instances\Container
classes or as an array.
$containers = $pod->getContainers();
foreach ($containers as $container) {
$container->getImage(); // mysql:5.7
}
To retrieve the containers and init containers as an array, pass false
to the retrieval method:
$containers = $pod->getContainers(false);
foreach ($containers as $container) {
$container['image'] // mysql:5.7
}
Pods can contain logs, and PHP K8s is good at it. Before checking how it works, please see the Live Tracking section from README to see how the closures really work at interpreting the real-time data in Kubernetes.
Retrieve a single string with all logs until the point of call:
// Simple logging, no watcher
// Returns a long string with the logs
$logs = $pod->logs();
Open up a websocket connection and watch for changes, line-by-line:
// Runs indefinitely until true/false
// us returned in the closure.
$pod->watchLogs(function ($line) {
// Process the logic here
// with the given line.
});